Issues with Unicode to UTF-8 Conversion in C using iconv Library

62 views Asked by At

I am attempting to convert a Unicode character represented as "\u5317" to its corresponding UTF-8 character in C using the iconv library. In Ruby, the conversion is achieved using the following code:

["5317"].pack("H*").unpack("n*").pack("U*")

This successfully outputs the UTF-8 character "北". However, when trying to accomplish the same task in C using the iconv library, the output is not as expected. Here is the C code snippet I am using:

#include <stdio.h>
#include <iconv.h>
#include <string.h>
#include <stddef.h>

int main() {
    iconv_t cd = iconv_open("UTF-8", "ASCII");
    char* inbuf = "5317";
    size_t inbytesleft = strlen(inbuf);
    char dest_str[100];
    char* outbuf = dest_str;
    size_t outbytesleft = sizeof dest_str;

    if (iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft) != (size_t)(-1)) {
        printf("success: %s\n", outbuf);
    } else {
        printf("failed\n");
    }

    return 0;
}

The output of this C code is "success:", but it does not display the expected UTF-8 character. I suspect there might be an issue with my usage of the iconv library or the choice of encoding. Can anyone guide me on the correct way to achieve this conversion in C using iconv or suggest an alternative approach?

0

There are 0 answers