I have an application which is compiled with gcc -fshort-wchar
flag. I can't remove that flag because some part of the code will not compile. I would like to convert wchar_t array to char array but because of that flag I can't use wcstombs
from the standard library. How to perform conversion ?

- 262,606
- 27
- 330
- 524

- 1,432
- 1
- 13
- 39
-
2Why can't you use wctombs? Have you tried the C version? (You may have to manually declare it) – Yakk - Adam Nevraumont Aug 23 '22 at 18:40
-
2`How to perform conversion ?` Write your own `wcstombs` or compile `wcstombs` with `short-wchar` or use some library. The question is too broad. `I can't remove that flag because some part of the code will not compile` that is odd. Why not just fix that part of code, so it compiles? Seems simpler. – KamilCuk Aug 23 '22 at 18:42
-
@Yakk - Adam Nevraumont [Here](https://stackoverflow.com/questions/15286568/implication-of-using-fshort-wchar) is some explanation why the application which is compiled with that flag can't use `wcstombs`. – Irbis Aug 23 '22 at 18:52
-
You don't mention what platform is it on. Platforms where UTF-16 is the native string format (notably Cocoa and Windows) have OS-level Unicode-to-multibyte conversion functions. If the only codepage you ever need is UTF-8, writing your own is relatively simple, too. – Seva Alekseyev Aug 23 '22 at 18:58
-
@Seva Alekseyev Platform that's Ubuntu 18.04 x64. Moreover the application set locale this way: `setlocale(LC_CTYPE,"");` Could you please present some example `wcstombs` imlementation ? – Irbis Aug 23 '22 at 19:05
-
What codepage do you want the converted string in? Do you want to support non-Basic Multilingual Plane characters (e. g. emoji)? – Seva Alekseyev Aug 23 '22 at 19:19
-
On a more general note, what do you need the `char` string for? Judging by your question, the answer community will benefit from a larger context. – Seva Alekseyev Aug 23 '22 at 19:25
-
UTF-8 (english US). I don't need support for non-Basic Multilingual Plane characters. I just need to log some information as char string. – Irbis Aug 23 '22 at 19:35
-
@Irbis Is there a reason why you are forcing `wchar_t` to 16 bits, instead of just using `char16_t` instead? If your goal is just to convert UTF-16 to UTF-8, that is easy to implement by hand, if you don't want to use a pre-existing Unicode library. Also see [Implication of using -fshort-wchar](https://stackoverflow.com/questions/15286568/) – Remy Lebeau Aug 23 '22 at 20:12
-
FYI: https://stackoverflow.com/questions/21456926/how-do-i-convert-a-string-in-utf-16-to-utf-8-in-c – Seva Alekseyev Aug 24 '22 at 12:40
1 Answers
First, note that 99.9% of the time, doing a search and replace of wchar_t
for my16bitchar_t
then defining my16bitchar_t
to be some 16 bit integer type is easier than -fshort-wchar
.
Your wchar_t
array is 16 bit. The wcstombs
apple provides expects 32 bit characters.
You can link against the system libraries and access the C functions that expose 32 bit access "blindly" by relying on the fact that C linkage doesn't include argument types, and that 16 bit integers are compatible even if their types don't match using the system calling convention.
This will require you to convert your UTF 16 characters to UTF 32 characters. If you are presuming UCS-2 (2 byte, no extended characters) this is easy; if not, this is challenging.
You get to write your own small unicode library to do the conversion! Yay, good for you.
If you went with a global search and replace of wchar_t
for my16bitchar_t
, you are in luck; you can now use 3rd party libraries (like icu) that support both utf-16 and utf-32.
...
If you only care about 7 bit clean ASCII, you can literally just ignore all bits past the 7th one and get valid UTF8. Like:
char ascii_char = static_cast<char>(0x7f & utf16_char);
it acts horribly on anything that isn't ascii however.

- 262,606
- 27
- 330
- 524
-
1In my case I can cast `wchart_t` to `char16_t` and use `c16rtomb` function. – Irbis Aug 25 '22 at 09:06