We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi!
I am using 2-byte wide char strings in my application. Strings should be written and read from file (in flash FS). When using fwprintf(), i got in file only one byte per character, like converting UTF-8 to plain ASCII.
Here's my simple example:
int write_setup(DEVICE *bt_s) { FILE *fout; fout = fopen (INI_FILE,"w"); if (fout != NULL) { fwprintf (fout, L"NAME=%ls\n", bt_s->name); fclose (fout); return 0; } else return -1; }
When I call this function, the bt_s->name points to memory with following contents:
0x54 0x00 0x65 0x00 0x73 0x00 0x74 0x00 or in ascii: T<0>e<0>s<0>t<0>
when I check the file contents (with file hex editor), the ontents is plain ascii, one byte per character: 0x54 0x65 0x73 0x74, which is "Test"
Is this compiler related issue on using wide characters? What is proper use on file-wirting (and reading) wchar_t strings ?
Thanks!
Marko
Wide characters are converted to multibyte representation when written to a file. This requires selecting proper locale which can be done at link time or at runtime.
The easiest way is to select the UTF-8 multibyte encoding by adding the following into your code:
#pragma import(__use_utf8_ctype)