none
Convert wchar_t to uint16

    Question

  • Dear all,

    I need to convert wchar_t to uint16 type and vice-versa. plz anyone help

    Monday, December 12, 2011 11:42 AM

Answers

  • I am developing a project (for both windows and MAC OS) that uses xerces for xml manipulation. So in windows I had no problem since XMLCh is the typedef of wchar_t. But whereas in LINUX/MACOS platform, XMLCh is the typedef of uint16_t. I have used wchar_t in many places in my project. So there I could pass in a string (eg. L"HAI this is for testing") and getback it with no change. But in LINUX/MAC OS how to convert the same wstring (L"HAI this is for testing") to uint16_t* and again getback the string from uint16_t* to wchar_t* with no change in string.
    Converting uint16_t* to wchar_t* is not the same as conveting uint16_t to wchar_t.
     
    uint16_t and wchar_t are both integer types, so you should be able to convert between them using static_cast(), even if they are not the same size (provided of course that the value in question fits in the target representation).
     
    Converting uint16_t* to wchar_t* will require a reinterpret_cast() (or C cast), and will only work if both are the same size on your platform. If they are not the same size, then you will need to convert each character.
     
    Is wchar_t not 16 bits on Mac/Linux?
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Rob Pan Monday, December 19, 2011 8:48 AM
    Tuesday, December 13, 2011 12:55 PM
  • Prabas08 wrote:

    I am developing a project (for both windows and MAC OS) that uses  xerces for xml manipulation. So in windows I had no problem
    since*XMLCh* is the typedef of wchar_t.But whereas in LINUX/MACOS  platform, *XMLCh *is the typedef ofuint16_t. I have used
    wchar_tin many places in my project. So there I could pass in a string  (eg. L"HAI this is for testing") and getback it with no
    change. But in LINUX/MAC OS how to convert the samewstring (*L"HAI  this is for testing"*) touint16_t* and again getback the
    string from uint16_t* to wchar_t* with no change in string.   

    If I recall correctly (it's been more than 10 years), with Xerces-C you  are supposed so use XMLString::transcode("HAI") (and then later  XMLString::release on the returned buffer). You can't use wchar_t or  L"string", since GCC defines wchar_t as 32-bit large, while Xerces-C  does everything in UTF-16.


    Igor Tandetnik

    • Marked as answer by Rob Pan Monday, December 19, 2011 8:48 AM
    Tuesday, December 13, 2011 1:33 PM

All replies

  • >I need to convert wchar_t to uint16 type and vice-versa. plz anyone help

    If on the platform you're using both are just 16-bit values, all you
    will need is a cast.

    Dave

    Monday, December 12, 2011 12:20 PM
  • Convert meaning what, exactly?  i.e. do you want to treat a single wchat_t object as a uint16?  If so, then just cast, as David suggests.

    Or do you want to convert a string of wchar_t characters into a unit16 by parsing the text, e.g. to convert L"123" into the unit16 value 123?  If this is what you're looking for, check out _wtoi and similar functions for the wchar-to-uint conversion, and _itow and similar functions for the unit to wchar conversion.  If you're using the C++ standard library, you can accomplish both conversions using std::basic_stringstream (particularly the typdefs wistringstream and wostringstream).


    -cd Mark the best replies as answers!
    Monday, December 12, 2011 4:20 PM
  • I am developing a project (for both windows and MAC OS) that uses xerces for xml manipulation. So in windows I had no problem since XMLCh is the typedef of wchar_t. But whereas in LINUX/MACOS platform, XMLCh is the typedef of uint16_t. I have used wchar_t in many places in my project. So there I could pass in a string (eg. L"HAI this is for testing") and getback it with no change. But in LINUX/MAC OS how to convert the same wstring (L"HAI this is for testing") to uint16_t* and again getback the string from uint16_t* to wchar_t* with no change in string.


    • Edited by Prabas08 Tuesday, December 13, 2011 7:08 AM
    Tuesday, December 13, 2011 7:06 AM
  • I am developing a project (for both windows and MAC OS) that uses xerces for xml manipulation. So in windows I had no problem since XMLCh is the typedef of wchar_t. But whereas in LINUX/MACOS platform, XMLCh is the typedef of uint16_t. I have used wchar_t in many places in my project. So there I could pass in a string (eg. L"HAI this is for testing") and getback it with no change. But in LINUX/MAC OS how to convert the same wstring (L"HAI this is for testing") to uint16_t* and again getback the string from uint16_t* to wchar_t* with no change in string.
    Converting uint16_t* to wchar_t* is not the same as conveting uint16_t to wchar_t.
     
    uint16_t and wchar_t are both integer types, so you should be able to convert between them using static_cast(), even if they are not the same size (provided of course that the value in question fits in the target representation).
     
    Converting uint16_t* to wchar_t* will require a reinterpret_cast() (or C cast), and will only work if both are the same size on your platform. If they are not the same size, then you will need to convert each character.
     
    Is wchar_t not 16 bits on Mac/Linux?
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Rob Pan Monday, December 19, 2011 8:48 AM
    Tuesday, December 13, 2011 12:55 PM
  • davewilk [MVP] wrote:

    Is wchar_t not 16 bits on Mac/Linux?

    It's not. For reasons I could never understand, GCC chose to make  wchar_t 32 bits, with UTF-32 in mind. Pretty much every major library  that deals with Unicode strings defines its own wide char type as  unsigned short, because of this.


    Igor Tandetnik

    Tuesday, December 13, 2011 1:09 PM
  • Prabas08 wrote:

    I am developing a project (for both windows and MAC OS) that uses  xerces for xml manipulation. So in windows I had no problem
    since*XMLCh* is the typedef of wchar_t.But whereas in LINUX/MACOS  platform, *XMLCh *is the typedef ofuint16_t. I have used
    wchar_tin many places in my project. So there I could pass in a string  (eg. L"HAI this is for testing") and getback it with no
    change. But in LINUX/MAC OS how to convert the samewstring (*L"HAI  this is for testing"*) touint16_t* and again getback the
    string from uint16_t* to wchar_t* with no change in string.   

    If I recall correctly (it's been more than 10 years), with Xerces-C you  are supposed so use XMLString::transcode("HAI") (and then later  XMLString::release on the returned buffer). You can't use wchar_t or  L"string", since GCC defines wchar_t as 32-bit large, while Xerces-C  does everything in UTF-16.


    Igor Tandetnik

    • Marked as answer by Rob Pan Monday, December 19, 2011 8:48 AM
    Tuesday, December 13, 2011 1:33 PM