Convert UTF-16 to UTF-8 under Windows and Linux, in C
67,431
Solution 1
Thanks guys, this is how I managed to solve the 'cross' windows and linux requirement:
- Downloaded and installed:
MinGW
, andMSYS
- Downloaded the
libiconv
source package - Compiled
libiconv
viaMSYS
.
That's about it.
Solution 2
Change encoding to UTF-8 with PowerShell:
Get-Content PATH\temp.txt -Encoding Unicode | Set-Content -Encoding UTF8 PATH2\temp.txt
Solution 3
If you don't want to use ICU,
- Windows: WideCharToMultiByte
- Linux: iconv (Glibc)
Solution 4
The open source ICU library is very commonly used.
Solution 5
#include <iconv.h>
wchar_t *src = ...; // or char16_t* on non-Windows platforms
int srclen = ...;
char *dst = ...;
int dstlen = ...;
iconv_t conv = iconv_open("UTF-8", "UTF-16");
iconv(conv, (char*)&src, &srclen, &dst, &dstlen);
iconv_close(conv);
Author by
DooriBar
Updated on August 21, 2021Comments
-
DooriBar over 2 years
I was wondering if there is a recommended 'cross' Windows and Linux method for the purpose of converting strings from UTF-16LE to UTF-8? or one should use different methods for each environment?
I've managed to google few references to 'iconv' , but for somreason I can't find samples of basic conversions, such as - converting a wchar_t UTF-16 to UTF-8.
Anybody can recommend a method that would be 'cross', and if you know of references or a guide with samples, would very appreciate it.
Thanks, Doori Bar