Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I read Unicode-16 strings from a file using POSIX methods in Linux?
    primarykey
    data
    text
    <p>I have a file containing UNICODE-16 strings that I would like to read into a Linux program. The strings were written raw from Windows' internal WCHAR format. (Does Windows always use UTF-16? e.g. in Japanese versions)</p> <p>I believe that I can read them using raw reads and the converting with wcstombs_l. However, I cannot figure what locale to use. Runing "locale -a" on my up-to-date Ubuntu and Mac OS X machines yields zero locales with utf-16 in their names.</p> <p>Is there a better way?</p> <p>Update: the correct answer and others below helped point me to using libiconv. Here's a function I'm using to do the conversion. I currently have it inside a class that makes the conversions into a one-line piece of code.</p> <pre><code>// Function for converting wchar_t* to char*. (Really: UTF-16LE --&gt; UTF-8) // It will allocate the space needed for dest. The caller is // responsible for freeing the memory. static int iwcstombs_alloc(char **dest, const wchar_t *src) { iconv_t cd; const char from[] = "UTF-16LE"; const char to[] = "UTF-8"; cd = iconv_open(to, from); if (cd == (iconv_t)-1) { printf("iconv_open(\"%s\", \"%s\") failed: %s\n", to, from, strerror(errno)); return(-1); } // How much space do we need? // Guess that we need the same amount of space as used by src. // TODO: There should be a while loop around this whole process // that detects insufficient memory space and reallocates // more space. int len = sizeof(wchar_t) * (wcslen(src) + 1); //printf("len = %d\n", len); // Allocate space int destLen = len * sizeof(char); *dest = (char *)malloc(destLen); if (*dest == NULL) { iconv_close(cd); return -1; } // Convert size_t inBufBytesLeft = len; char *inBuf = (char *)src; size_t outBufBytesLeft = destLen; char *outBuf = (char *)*dest; int rc = iconv(cd, &amp;inBuf, &amp;inBufBytesLeft, &amp;outBuf, &amp;outBufBytesLeft); if (rc == -1) { printf("iconv() failed: %s\n", strerror(errno)); iconv_close(cd); free(*dest); *dest = NULL; return -1; } iconv_close(cd); return 0; } // iwcstombs_alloc() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload