Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Converting ANSI to UNICODE

Converting ANSI to UNICODE

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bhanu_reddy09
    wrote on last edited by
    #1

    Dear All, I am having problem in converting ANSI to UNICODE but my code is not giving the desired result. Please have a look at the below code and advice me. When I compare the both strings, the strings are not equal after conversion. Please help me. Many thanks in advance.

    char szData[2] ;
    szData[0] = '♠';
    szData[1] = 0;
    wchar_t wszData1[] = L"♠";
    wchar_t wszData2[2];
    MultiByteToWideChar(CP_ACP, 0, szData, -1, wszData2, sizeof(wszData2) / sizeof wchar_t));
    if (wcscmp(wszData1, wszData2) == 0)
    MessageBox("Strings are equal!");
    else
    MessageBox("Strings are not equal.");

    CPalliniC M 2 Replies Last reply
    0
    • B bhanu_reddy09

      Dear All, I am having problem in converting ANSI to UNICODE but my code is not giving the desired result. Please have a look at the below code and advice me. When I compare the both strings, the strings are not equal after conversion. Please help me. Many thanks in advance.

      char szData[2] ;
      szData[0] = '♠';
      szData[1] = 0;
      wchar_t wszData1[] = L"♠";
      wchar_t wszData2[2];
      MultiByteToWideChar(CP_ACP, 0, szData, -1, wszData2, sizeof(wszData2) / sizeof wchar_t));
      if (wcscmp(wszData1, wszData2) == 0)
      MessageBox("Strings are equal!");
      else
      MessageBox("Strings are not equal.");

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      bhanu_reddy09 wrote:

      szData[0] = '♠';

      How could it work?

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • B bhanu_reddy09

        Dear All, I am having problem in converting ANSI to UNICODE but my code is not giving the desired result. Please have a look at the below code and advice me. When I compare the both strings, the strings are not equal after conversion. Please help me. Many thanks in advance.

        char szData[2] ;
        szData[0] = '♠';
        szData[1] = 0;
        wchar_t wszData1[] = L"♠";
        wchar_t wszData2[2];
        MultiByteToWideChar(CP_ACP, 0, szData, -1, wszData2, sizeof(wszData2) / sizeof wchar_t));
        if (wcscmp(wszData1, wszData2) == 0)
        MessageBox("Strings are equal!");
        else
        MessageBox("Strings are not equal.");

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        szData[0] = '♠';
        wchar_t wszData1[] = L"♠";

        These probably aren't doing what you're expecting. You should only have ASCII characters in source code, and use hex escapes for any characters outside the ASCII range. Depending on what that character is, and what your system's code page is, it may not even be possible to store the character in a non-Unicode string.

        --Mike-- Dunder-Mifflin, this is Pam.

        B S 2 Replies Last reply
        0
        • M Michael Dunn

          szData[0] = '♠';
          wchar_t wszData1[] = L"♠";

          These probably aren't doing what you're expecting. You should only have ASCII characters in source code, and use hex escapes for any characters outside the ASCII range. Depending on what that character is, and what your system's code page is, it may not even be possible to store the character in a non-Unicode string.

          --Mike-- Dunder-Mifflin, this is Pam.

          B Offline
          B Offline
          bhanu_reddy09
          wrote on last edited by
          #4

          Many thanks for your replies and suggestions. Actually I read a data from the serial device and it is having the ASCII value 6 and the corresponding character for 6 is '♠'. I just give the previous code as an example.

          //the actual data will be
          szData[0] = 0x06;//from a serial device

          Now, I want to display the corresponding character (received from serial device) for 0x06 in the CEdit. Please advice me.

          CPalliniC 1 Reply Last reply
          0
          • M Michael Dunn

            szData[0] = '♠';
            wchar_t wszData1[] = L"♠";

            These probably aren't doing what you're expecting. You should only have ASCII characters in source code, and use hex escapes for any characters outside the ASCII range. Depending on what that character is, and what your system's code page is, it may not even be possible to store the character in a non-Unicode string.

            --Mike-- Dunder-Mifflin, this is Pam.

            S Offline
            S Offline
            Souldrift
            wrote on last edited by
            #5

            This line

            szData[0] = '♠';

            produces a compiler warning doesn´t it? ♠ is not an ANSI character, afaik. If you use an actual ansi sign like '®' in your code, it works properly. Souldrift

            B 1 Reply Last reply
            0
            • S Souldrift

              This line

              szData[0] = '♠';

              produces a compiler warning doesn´t it? ♠ is not an ANSI character, afaik. If you use an actual ansi sign like '®' in your code, it works properly. Souldrift

              B Offline
              B Offline
              bhanu_reddy09
              wrote on last edited by
              #6

              Yes, I got compiler warning but how to change the character 0x06 to Unicode and display properly in the CEdit. Thanks...

              S 1 Reply Last reply
              0
              • B bhanu_reddy09

                Yes, I got compiler warning but how to change the character 0x06 to Unicode and display properly in the CEdit. Thanks...

                S Offline
                S Offline
                Souldrift
                wrote on last edited by
                #7

                I am not sure if this can be done since 0x06 (actually the first 32 codes) are designated control characters. 0x06 being ACK code for data transmission, I think. Sorry to not be of more help. Souldrift Edit: http://www.handheld-basic.com/documentation/text/page_599.html[^]

                B 1 Reply Last reply
                0
                • B bhanu_reddy09

                  Many thanks for your replies and suggestions. Actually I read a data from the serial device and it is having the ASCII value 6 and the corresponding character for 6 is '♠'. I just give the previous code as an example.

                  //the actual data will be
                  szData[0] = 0x06;//from a serial device

                  Now, I want to display the corresponding character (received from serial device) for 0x06 in the CEdit. Please advice me.

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  bhanu_reddy09 wrote:

                  szData[0] = 0x06;//from a serial device

                  This is a ASCII control character (ACK, see [^]), i.e. not printable. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • S Souldrift

                    I am not sure if this can be done since 0x06 (actually the first 32 codes) are designated control characters. 0x06 being ACK code for data transmission, I think. Sorry to not be of more help. Souldrift Edit: http://www.handheld-basic.com/documentation/text/page_599.html[^]

                    B Offline
                    B Offline
                    bhanu_8509
                    wrote on last edited by
                    #9

                    Dear All, I have tried in many possible ways but I cannot display the character ♠. The real problem is that I need to change my entire project to UNICODE but this is not possible for right now. I also tried the below and even not working.

                    wchar_t display[2];
                    display[0] = 0x2660;
                    display[1] = 0;
                    edit.SetWindowText(display); //This only works when I change the project into UNICODE.

                    Is there any possible way to do simply. Many thanks in advance.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups