CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2005
    Posts
    65

    Unicode string (japanese) to ANSI

    Hi,

    I have written a C++ console app using VS2005 which has these two lines.

    CString csSrc = _T("C:\\管理者"); // Japanese characters
    char* pDst = T2A(csSrc);

    when I run this program and watch the values during debugging, pDst shows "C:\管ç*者".

    what am I missing here ?
    what changes should I make so that pDst points to "C:\\管理者".

    Thanks in advance.

  2. #2
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Unicode string (japanese) to ANSI

    Hi,

    As I understand it, "C:\\管理者". will be a double-byte string however it is represented (Unicode, MBCS, etc.), there is no codepage where you can map these characters to single-bytes.

    So, you can't really get char *pDst to point to "C:\\管理者" and see it like that in the debugger.

    Can you explain more about what you're trying to do?

    Depending on what you want to do, the answer is maybe to define pDst as TCHAR. Alternatively, you can get the string buffer using scSrc.GetBuffer(), and work on the raw bytes if that's what you're trying to do.

    Alan

  3. #3
    Join Date
    Aug 2005
    Posts
    65

    Re: Unicode string (japanese) to ANSI

    Thanks for the reply.

    here is the use case,

    I need to pass "管理者" which is stored in a CString to an API ( third party library)which takes char* as the input.

    Any ideas ?

  4. #4
    Join Date
    Nov 2007
    Posts
    74

    Re: Unicode string (japanese) to ANSI

    You need to change them into proper type before doing so
    Checkout APIs for string conversion Multibyte string to ANSI as well as ANSItoMUltibyte

  5. #5
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: Unicode string (japanese) to ANSI

    Hi,

    Sorry, I don't have an idea for that, I can't see how the library can handle Japanese characters if it only accepts a char*. Maybe you can get a unicode version of the library?

    Alan

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Unicode string (japanese) to ANSI

    Using extended characters in string literals isn't a good idea: http://www.codeguru.com/forum/showpo...8&postcount=14

    >> to an API ( third party library) which takes char* as the input.
    If the API doesn't accept UTF8 encoded strings, or a string using code page 932 (Shift-JIS) - then you're out of luck.

    gg

  7. #7
    Join Date
    Aug 2005
    Posts
    65

    Re: Unicode string (japanese) to ANSI

    Thanks for the reply guys.

    How do I convert a wide char string to UTF8 format ?

    Any links would be helpful.

  8. #8
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Unicode string (japanese) to ANSI

    You use MultiByteToWideChar() to go from CodePage char string, or UTF8 char string, to UTF16LE wchar_t string.
    You use WideCharToMultiByte() to go from wchar_t string to CodePage or UTF8 char string.

    Sample code: http://www.codeguru.com/forum/showpo...1&postcount=11
    When calling wstr_to_str(), pass in CP_UTF8 to convert to UTF8.

    gg

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured