In C/C++, if a multi-byte wide character (wchar_t) value is transmitted from a big-endian system to a little-endian system (or vice-versa), will it come out the same value on the other side? Or will the bytes need to be swapped?
C++ – Is endian conversion required for wchar_t data
- C++ – How to convert a value from host byte order to little endian
- C++ – What are the basic rules and idioms for operator overloading
- C++ – Image Processing: Algorithm Improvement for ‘Coca-Cola Can’ Recognition
- Convert Little Endian to Big Endian
- Linux – What does htons() do on a Big-Endian system
- C++ – Compiling an application for use in highly radioactive environments
- C/C++ code to convert big endian to little endian
Yes you will need to swap them.
The bytes will be retrieved from the transport in the same order they were put in. Just at the other end the ordering of these bytes has a different meaning. So you need to convert them to the correct endian-ness (is that a word?).
The tried and true method is to convert to network byte order before transport. Then convert back to host specific byte order (from network byte order) on receipt.
A set of function to help with endian conversion:
Just to add another note of caution.
Different systems use different size for wchar_t so do not assume sizeof(wchar_t) == 2.
Additionally each host may use a different representational format for wchar_t.
To help deal with this most systems convert the text to a known format for transport (UTF-8 or UTF-16 are good choices). The convert the text back to the host specific format at the other end.
You could look at IBM's icu this has all this functionality.