I have a function which requires me to pass a UTF-8 string pointed by a char*, and I have the char pointer to a single-byte string. How can I convert the string to UTF-8 encoding in C++? Is there any code I can use to do this?
Thanks!
I have a function which requires me to pass a UTF-8 string pointed by
Share
To convert a string to a different character encoding, use any of various character encoding libraries. A popular choice is iconv (the standard on most Linux systems).
However, to do this you first need to figure out the encoding of your input. There is unfortunately no general solution to this. If the input does not specify its encoding (like e.g. web pages generally do), you’ll have to guess.
As to your question: You write that you get the string from calling
readdiron a FAT32 file system. I’m not quite sure, but I believereaddirwill return the file names as they are stored by the file system. In the case of FAT/FAT32:If you use the standard
vfatLinux kernel module to access the FAT32 partition, you should get long file names fromreaddir(unless a file only has an 8.3 name).These can be decoded as UTF-16.FAT32 stores the long file names in UTF-16 internally. Thevfatdriver will convert them to the encoding given by theiocharset=mount parameter (with the default being the default system encoding, I believe).Additional information:
You may have to play with the mount options
codepageandiocharset(see http://linux.die.net/man/8/mount ) to get filenames right on the FAT32 volume. Try to mount such that filenames are shown correctly in a Linux console, then proceed. There is some more explanation here: http://www.nslu2-linux.org/wiki/HowTo/MountFATFileSystems