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. convert const wchar* to const byte*

convert const wchar* to const byte*

Scheduled Pinned Locked Moved C / C++ / MFC
help
20 Posts 6 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.
  • C CPallini

    Your code performs the conversion (i.e. the cast) of the pointer, not of the string. In other words the string buffer remains unchanged, it is just interpreted diffently. Since that maybe fine or maybe a plain catastrophe, I need to know what is the purpose of your 'conversion'. :)

    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]

    R Offline
    R Offline
    Rajesh R Subramanian
    wrote on last edited by
    #10

    CPallini wrote:

    I need to know what is the purpose of your 'conversion'.

    Just noticed t hat we both have put that word in quotes. Fits the context perfectly. Subtly expressing something, ya know. :-D

    It is a crappy thing, but it's life -^ Carlo Pallini

    1 Reply Last reply
    0
    • R Rajesh R Subramanian

      Sure, you can reinterpret a pointer to a dialog as a pointer to a socket. It is possible to achieve that. But WHY?! After such a conversion (assuming you did it), what will you want to be doing with the BYTE buffer?! Answer that.

      It is a crappy thing, but it's life -^ Carlo Pallini

      R Offline
      R Offline
      Rakesh5
      wrote on last edited by
      #11

      Hi All, I am actually learning vc++.. i just want to know whether it can be converted or not.. whether it is feasible or not.. my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone.. if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone.. Hence raised in this forum whether you people can enlighten me about this issue.. thanks, rakesh.

      C R 2 Replies Last reply
      0
      • R Rakesh5

        Hi All, I am actually learning vc++.. i just want to know whether it can be converted or not.. whether it is feasible or not.. my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone.. if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone.. Hence raised in this forum whether you people can enlighten me about this issue.. thanks, rakesh.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #12

        Rakesh5 wrote:

        if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone..

        If you do need to convert the wide char string into a char one, then use a conversion macro instead, for instance (assuming a ANSI build, like yours):

        const wchar_t * wstrTest = L"Hello";
        CW2A strTest( wstrTest );
        AfxMessageBox( strTest);

        Rakesh5 wrote:

        my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..

        I hope your seniors know what they are doing. :)

        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]

        R 1 Reply Last reply
        0
        • R Rakesh5

          Hi All, I am actually learning vc++.. i just want to know whether it can be converted or not.. whether it is feasible or not.. my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone.. if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone.. Hence raised in this forum whether you people can enlighten me about this issue.. thanks, rakesh.

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #13

          A BYTE is an 8 bit data type. A WCHAR is a 16 bit data type. Now, you can cast a BYTE pointer to a WCHAR pointer or vice versa. However, since these data types are of different sizes, and if you cast an array of BYTES to an array of WCHARs, the conversion will succeed, but the resultant buffer will be unusable (the code "won't work"). In other words, you only casted the pointer. You can call a horse a dog, but however, it won't bark. OK, I know. That's not a good example, but you get the drift. :)

          Rakesh5 wrote:

          my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..they are sending type-cast parameter as an arugment to other functions..but it is not casting properly....its taking the first character alone..

          I have a feeling that your seniors are not the brightest sparks for you to learn from. Or ask them to give you an explanation of what they're trying to achieve and ask them how such a "conversion" could make sense.

          It is a crappy thing, but it's life -^ Carlo Pallini

          1 Reply Last reply
          0
          • R Rakesh5

            Hi, i am trying to convert const wchar* to const byte*. but its not converting fully...can anyone help me out to solve this issue... ------------------------------------- code snippet: const WCHAR* test = L"hello"; const BYTE* test1 = (const BYTE*) test; AfxMessageBox((CString)test1); ---------------------------------------------- thanks, rakesh

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #14

            const BYTE* test1 = (const BYTE*) test; is a type cast, it means your are changing the pointer type. Search for "String Conversion". It is a wide area. There are some fine ATL-macros: http://msdn.microsoft.com/en-us/library/87zae4a3%28VS.80%29.aspx[^] :suss:

            Press F1 for help or google it. Greetings from Germany

            1 Reply Last reply
            0
            • C CPallini

              Rakesh5 wrote:

              if i say, const wchar* test = L"hello"; const BYTE* test1 = (const BYTE*)test; o/p will be 'h' alone..

              If you do need to convert the wide char string into a char one, then use a conversion macro instead, for instance (assuming a ANSI build, like yours):

              const wchar_t * wstrTest = L"Hello";
              CW2A strTest( wstrTest );
              AfxMessageBox( strTest);

              Rakesh5 wrote:

              my seniors have used a functions in which they have typecast like wat i have mentioned in the first message..

              I hope your seniors know what they are doing. :)

              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]

              R Offline
              R Offline
              Rakesh5
              wrote on last edited by
              #15

              Hi, Actually i have made my character set as unicode and not as multibyte in the project settings.. so its not converting fully.. is there any way to do by keeping the current settings? thanks, rakesh.

              C 1 Reply Last reply
              0
              • R Rakesh5

                Hi, Actually i have made my character set as unicode and not as multibyte in the project settings.. so its not converting fully.. is there any way to do by keeping the current settings? thanks, rakesh.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #16

                Then use

                const wchar_t * wstrTest = L"Hello";
                CW2A strTest( wstrTest );
                AfxMessageBox( (CString) strTest);

                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]

                1 Reply Last reply
                0
                • R Rakesh5

                  Hi, i am trying to convert const wchar* to const byte*. but its not converting fully...can anyone help me out to solve this issue... ------------------------------------- code snippet: const WCHAR* test = L"hello"; const BYTE* test1 = (const BYTE*) test; AfxMessageBox((CString)test1); ---------------------------------------------- thanks, rakesh

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #17

                  Simply casting an 8-bit type to a 16-bit type is not going to produce the expected result. Consider:

                  8 8 8 8 8
                  ╓─╥─╥─╥─╥─╖
                  ║H║e║l║l║o║
                  ╙─╨─╨─╨─╨─╜

                  16 16 16 16 16
                  ╓─┬─╥─┬─╥─┬─╥─┬─╥─┬─╖
                  ║H│0║e│0║l│0║l│0║o│0║
                  ╙─┴─╨─┴─╨─┴─╨─┴─╨─┴─╜

                  In the top figure, each character uses 1 byte or 8 bits. In the bottom figure, each character uses 2 byte or 16 bits. The second byte holds a \0 character. Casting will not magically add that character.

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  R 1 Reply Last reply
                  0
                  • D David Crow

                    Simply casting an 8-bit type to a 16-bit type is not going to produce the expected result. Consider:

                    8 8 8 8 8
                    ╓─╥─╥─╥─╥─╖
                    ║H║e║l║l║o║
                    ╙─╨─╨─╨─╨─╜

                    16 16 16 16 16
                    ╓─┬─╥─┬─╥─┬─╥─┬─╥─┬─╖
                    ║H│0║e│0║l│0║l│0║o│0║
                    ╙─┴─╨─┴─╨─┴─╨─┴─╨─┴─╜

                    In the top figure, each character uses 1 byte or 8 bits. In the bottom figure, each character uses 2 byte or 16 bits. The second byte holds a \0 character. Casting will not magically add that character.

                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #18

                    Nice illustration, David. But the Unicode string in the figure seem to have been terminated by a single '0' character, whereas it should be 2 '0's. :)

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    D 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      Nice illustration, David. But the Unicode string in the figure seem to have been terminated by a single '0' character, whereas it should be 2 '0's. :)

                      It is a crappy thing, but it's life -^ Carlo Pallini

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #19

                      Rajesh R Subramanian wrote:

                      But the Unicode string in the figure seem to have been terminated...

                      Actually, neither is terminated. I didn't deem that important.

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      R 1 Reply Last reply
                      0
                      • D David Crow

                        Rajesh R Subramanian wrote:

                        But the Unicode string in the figure seem to have been terminated...

                        Actually, neither is terminated. I didn't deem that important.

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #20

                        Oh yeah. I only looked at the Unicode text. :-O And yes, the illustration serves the purpose well.

                        It is a crappy thing, but it's life -^ Carlo Pallini

                        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