CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jun 2015
    Posts
    13

    how to pass char* to wchar_t*

    Hi! I am getting a string like: "aña!a¡a¿a?a" from a server so I have to decode it and then pass it to a function.

    The header of my function is: void SetInfo(int num, char *descr[4]) so it receives one number and an array of 4 chars (sentences). To make it easier, let's say I just need to work only with descr[0] which has the string before mentioned.

    When I debug and arrive there to SetInfo(), I get the exact message in the debugg view: "aña!a¡a¿a?a" so until here is all ok. Initially, the info I was receiving on that function, was a std::wstring so all my code working with that message was with wstrings and strings but now what I receive is a char as shown in the header. The message arrived until here ok, but if I want to work with it, then I can't because if I debug and see each position of Descr[0] then I get

    Code:
     descr[0][0] = 'a'; //ok
     descr[0][1] = 'Ã '; // BAD
    so I tried converting char* to wchar* with a code found here:

    Code:
     size_t size = strlen(descr[0]) + 1;
     wchar_t* wa = new wchar_t[size];
     mbstowcs(wa,descr[0],size);
    But then the debugger shows me that wa has:

    Code:
    wa wchar_t *   0x185d4be8 L"a-\uffffffff刯2e2e牵6365⽳6f73歯6f4c楲6553䈯736f獵6e6f档6946琯7361灭6569湰2e6f琀0067\021ᡰ9740슃b8\020\210=r"
    which I suppose that is incorrect (I'm supossing that I have to see the same initial message of "aña!a¡a¿a?a". If this message is fine then I don't know how to get what I need...)

    So my question is: how can I get that descr[0][0] = 'a' and descr[0][1] = 'ñ' ?? I can't pass char to wchar (you've already see what I got). Am I doing it wrong? Or is there any other way? I am really stuck on that so any idea will be very apreciated.

    Thank you so much

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

    Re: how to pass char* to wchar_t*

    >> I am getting a string like: "aña!a¡a¿a?a" from a server so I have to decode it and then pass it to a function.
    The first thing you need to figure out is how that byte string is encoded. If you don't know, tell us the individual byte values of that string.

    gg

  3. #3
    Join Date
    Jun 2015
    Posts
    13

    Re: how to pass char* to wchar_t*

    I know that they use a function for it but nothing more. How can I get the byte values? I suppose you mean the binary part or something like that but no idea about how to get it (I use eclipse)

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

    Re: how to pass char* to wchar_t*

    Use the debugger. Get the integer values for descr[0][0], [0][1], ..., [0][N].
    a=...
    ñ=...
    etc.

    gg

  5. #5
    Join Date
    Jun 2015
    Posts
    13

    Re: how to pass char* to wchar_t*

    Hi,

    When I debug, if the text is "aña" and I put descr[0][1] in the debugger, it says that is a char type and the value is -61 'Ã' ... is that what you are asking for?
    However, descr[0] gives me that is a char* type and then 0x8197a1c "aña"

    And for word "Euro" for example I get:
    Code:
    E = 69 'E'
    u = 117 'u'
    r = 114 'r'
    o = 111 'o'
    EDIT: the code -61 'Ã' is in that example. When I get the sentence from the server (another one but iwth a 'ñ' inside), then its -63 'Ã'
    Last edited by roseicollis; September 22nd, 2015 at 02:57 AM.

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

    Re: how to pass char* to wchar_t*

    So it looks like the byte encoding of your text is UTF8.

    "ñ" is encoded in UTF8 with two bytes: 0xC3 0xB2
    0xC3 is 195 - when represented as signed char, that's -61.

    Here's an easy way to convert it into a wchar_t string: http://forums.codeguru.com/showthrea...31#post1790131

    gg

  7. #7
    Join Date
    Jun 2015
    Posts
    13

    Re: how to pass char* to wchar_t*

    oh nice! Thanks for that explanation!! Now I have some problems: First my program does not recognize the MultiByteToWideChar function (I have #include <string>) and second, it doesnt recognize the CP_UTF8 and I don't see why... is it because I'm on eclipse on linux?

    Ok, I've see that MultiByteToWideChar is only for windows, and that the equivalent for linux should be something with mbstowcs that is what I am using, but as you see, it does not work fine... any idea?

    And another thing... why do I get -61 if I write the char array but a -63 if the char array comes from the server?

    --------- Maybe this helps:
    The server sends: "$ Euroñ$"
    When I decode I receive: "$ EuroÃ$"

    Then if I use mbstowcs, what I get in the debugger is " 0x185fdc50 L"$ Euro忳e8"
    Last edited by roseicollis; September 22nd, 2015 at 05:15 AM.

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

    Re: how to pass char* to wchar_t*

    Yes, the equivalent on *nix is libiconv.

    If you know that your default locale is using UTF8, then you can use mbstowcs by calling setlocale(LC_ALL, "").

    LATIN CAPITAL LETTER A WITH TILDE (Ã) is represented as 0xC3 in many single-byte codepages: http://www.tachyonsoft.com/uc0000.htm#U00C3

    I suspect that when you see "$ EuroÃ$", you are seeing code that is attempting to interpret the string in some single-byte codepage.
    ñ = 0xC3,0xB2 in UTF8. The 0xC3 can be interpreted as Ã, and perhaps the 0xB2 doesn't render.

    gg

  9. #9
    Join Date
    Jun 2015
    Posts
    13

    Re: how to pass char* to wchar_t*

    Hi codeplug,

    I think I was doing it totally wrong because I was receiving the buffer and decoding it with a function (My boss told me to use that function because it should work fine and I can't use another) so I use teh DecodeString() function which gaves me the char* I said before: "$ EuroÃ$" and then I was trying to pass that char* to the wchar_t* with the mbtowsc() function...

    Please correct me if I'm wrong but I think that I have to use that mbtowsc to decode it, the buffer, and not to pass the char to wchar... am I in the right way now?

    Now what I did is take the DecodeString() and copy it (to do the same) but instead of the memcpy() I use that mbtowsc function. Then, when I decode instead of getting "$ EuroÃ$", I get "$ Euro" and nothing more, not 'ñ' and not '$' any idea of why? (If I'm on the right way now ofc). And the length is ok because if I decode the length+1 for example there is a SIGSEGV

    Thank you so much
    Last edited by roseicollis; September 22nd, 2015 at 07:05 AM.

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

    Re: how to pass char* to wchar_t*

    >> ... not 'ñ' and not '$' any idea of why?
    Did you call setlocale(LC_ALL, "") at the beginning of the program?
    When you type "locale" on the command line, what does it output?

    gg

  11. #11
    Join Date
    Jun 2015
    Posts
    13

    Re: how to pass char* to wchar_t*

    Hi Code,

    The thing is that i should not use setlocale() ... I already needed it for another thing but my boss didn't allow me. I've put it to try if it changes anything but not, I still get the "$ Euro"

    When you type "locale" on the command line, what does it output?
    Do you mean on the OS konsole? or in eclipse? (If you mean on eclipse I don't know where should I put it)
    If yes, then that is what I get:

    LANG=ca_ES.UTF-8
    LC_CTYPE="ca_ES.UTF-8"
    LC_NUMERIC="ca_ES.UTF-8"
    LC_TIME="ca_ES.UTF-8"
    LC_COLLATE="ca_ES.UTF-8"
    LC_MONETARY="ca_ES.UTF-8"
    LC_MESSAGES="ca_ES.UTF-8"
    LC_PAPER="ca_ES.UTF-8"
    LC_NAME="ca_ES.UTF-8"
    LC_ADDRESS="ca_ES.UTF-8"
    LC_TELEPHONE="ca_ES.UTF-8"
    LC_MEASUREMENT="ca_ES.UTF-8"
    LC_IDENTIFICATION="ca_ES.UTF-8"
    LC_ALL=
    Sorry, I'm kinda new with eclipse and linux.

    Edit:

    With no changes made, I don't know why but after getting the "$ Euro" I close the program, run it again and then what I get is "$ Euroᡁ9b10ᡁ8e98ᡁ9170" and if I close it again and run it: $ Euro쏿65f3侦053c㵂37d0 or $ Euro\uff000000 ... so every time I get differents values after the Euro word.
    Last edited by roseicollis; September 22nd, 2015 at 11:21 AM.

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

    Re: how to pass char* to wchar_t*

    If you can't call setlocale, then you can't use mbstowcs.

    Use libiconv instead - eg: http://stackoverflow.com/questions/1...tring-to-utf-8

    gg

  13. #13
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to pass char* to wchar_t*

    Alternatively, a more "C++" like library, portable between windows and linux is UTF-8 CPP (http://sourceforge.net/projects/utfcpp/). This will give you actual utf encoded strings though, as opposed to raw char* wchar_t*. May or may not fit your needs.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  14. #14
    Join Date
    Jun 2015
    Posts
    13

    Re: how to pass char* to wchar_t*

    EDITED:

    Omg..... The person that makes the server part changed the text without telling me so he was not sending a 'ñ' .... -.-'' I think I'll commit suicide... 2 days for that and it's all wrong.... Now that I get the correct message, and a nice -15 'ñ' ....my problem is another:

    My goal is to be able to do something like:

    Code:
    for (int i = 0; i < total; i++)
    {
        if(descr[0][i]=='a') //do things
        else if (descr[0][i]=='ñ') // do other stuff  
    }
    This is what I am not able to get because descr[0][6] never equals 'ñ'...

    I've tried with:
    Code:
       if(descr[0][i]=='\x00D1')
       if(descr[0][i]=='\x00F1')
       if(descr[0][i]=='ñ') //this compiles but then ignores that if like it is an error
       if(descr[0][i]==L'ñ')
    But it enver enters on any of that ifs, so.. to what should I compare descr[0][i] to know if it is an 'ñ' ?
    Last edited by roseicollis; September 23rd, 2015 at 03:14 AM.

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to pass char* to wchar_t*

    Quote Originally Posted by roseicollis View Post
    My goal is to be able to do something like:

    Code:
    for (int i = 0; i < total; i++)
    {
        if(descr[0][i]=='a') //do things
        else if (descr[0][i]=='ñ') // do other stuff  
    }
    Please don't do that. The resulting compiled code will highly depend on the encode the file is saved with. Heck, it might not even compile on certain code pages (such as utf-8): Indeed, for certain encodings (such as UTF-8, 'ñ' is 2 chars wide. Heck, certain encodings don't even have 'ñ'...


    Quote Originally Posted by roseicollis View Post
    This is what I am not able to get because descr[0][6] never equals 'ñ'...

    I've tried with:
    Code:
       if(descr[0][i]=='\x00D1')
       if(descr[0][i]=='\x00F1')
       if(descr[0][i]=='ñ') //this compiles but then ignores that if like it is an error
       if(descr[0][i]==L'ñ')
    But it enver enters on any of that ifs, so.. to what should I compare descr[0][i] to know if it is an 'ñ' ?
    If your input is indeed utf-8 (which I belive it is), then you *can't* do what you are asking for, for the simple reason that 'ñ' is *not* encoded using a single codeunit (char).

    There are different ways to achieve what you are trying to do, but to be honest, I don't understand what or why you are trying to do it? Text manipulation is *very* difficult. The last thing you want to do is do it by hand.

    If you "just" convert your char* to wchar_t*, then you shouldn't have to special case 'ñ'. Also, note that once you *do* have a wchar_t*, the tests you are trying to do *should* work (though I still advise against doing them at all).
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Tags for this Thread

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