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 char* into BYTE*

converting char* into BYTE*

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
16 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.
  • CPalliniC CPallini

    Short (and dangerous) answer: with a cast, for instance

    char * myCharPointer ="hello";
    BYTE * myBytePointer = (BYTE *) myCharPointer;

    A better answer may follow a more detailed request. :)

    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]

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #5

    of course, you're looking for devil every where :p a char* is not always a string ;)

    char c = 'c';
    char* pc = &c;

    BYTE* pb1 = (BYTE*)pc;
    BYTE* pb2 = reinterpret_cast<BYTE*>(pc);

    BTW, looking at the level of the question, i think it's worth saying that a char IS a BYTE:

    char c = 'c';

    BYTE b = c;

    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    V CPalliniC 2 Replies Last reply
    0
    • T toxcct

      of course, you're looking for devil every where :p a char* is not always a string ;)

      char c = 'c';
      char* pc = &c;

      BYTE* pb1 = (BYTE*)pc;
      BYTE* pb2 = reinterpret_cast<BYTE*>(pc);

      BTW, looking at the level of the question, i think it's worth saying that a char IS a BYTE:

      char c = 'c';

      BYTE b = c;

      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      V Offline
      V Offline
      VaDa Um Uie
      wrote on last edited by
      #6

      toxcct wrote:

      a char* is not always a string

      char* is never a string. :laugh:

      T 1 Reply Last reply
      0
      • V VaDa Um Uie

        toxcct wrote:

        a char* is not always a string

        char* is never a string. :laugh:

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #7

        what do you know about C++, you Mr univoter ?

        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

        V 1 Reply Last reply
        0
        • T toxcct

          what do you know about C++, you Mr univoter ?

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          V Offline
          V Offline
          VaDa Um Uie
          wrote on last edited by
          #8

          nice arguments :laugh:

          1 Reply Last reply
          0
          • T toxcct

            Matthew Faithfull wrote:

            BYTE* pByte = reinterpretcast< BYTE* >( pChar );

            quite... reinterpret_cast is the correct word ^^ BTW, i didn't try, but wouldn't static_cast just work here ?

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            M Offline
            M Offline
            Matthew Faithfull
            wrote on last edited by
            #9

            Right you are, I knew that looked wrong somehow. I don't think static_cast works on pointers even when there are in fact a type match as these probably would be. Would have to try it to be sure.

            "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

            1 Reply Last reply
            0
            • S subramanyeswari

              Hi, how to conver char* into BYTE* in C++ Regards

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

              A BYTE is nothing but an unsigned char. If you do the math, that would tell you that the conversion may result in data loss, depending on the value stored in the char variable. The following situation may be an example:

              char p = -23;
              int i; // if you assign p to i, you'll not lose data.
              BYTE b; // if you assign p to b, you'll lose data.

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

              M 1 Reply Last reply
              0
              • T toxcct

                Matthew Faithfull wrote:

                BYTE* pByte = reinterpretcast< BYTE* >( pChar );

                quite... reinterpret_cast is the correct word ^^ BTW, i didn't try, but wouldn't static_cast just work here ?

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                toxcct wrote:

                BTW, i didn't try, but wouldn't static_cast just work here ?

                A static_cast won't work.

                Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  A BYTE is nothing but an unsigned char. If you do the math, that would tell you that the conversion may result in data loss, depending on the value stored in the char variable. The following situation may be an example:

                  char p = -23;
                  int i; // if you assign p to i, you'll not lose data.
                  BYTE b; // if you assign p to b, you'll lose data.

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                  M Offline
                  M Offline
                  Matthew Faithfull
                  wrote on last edited by
                  #12

                  To be very picky you don't actually loose data, as it's still an 8-bit value. It's just that the semantics change, your -23 = 11101001 gets reinterpretted as 233 = 11101001. :)

                  "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

                  R 1 Reply Last reply
                  0
                  • M Matthew Faithfull

                    To be very picky you don't actually loose data, as it's still an 8-bit value. It's just that the semantics change, your -23 = 11101001 gets reinterpretted as 233 = 11101001. :)

                    "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

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

                    [Footer: not for nitpicks, but for the noob op] :laugh:

                    Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Microsoft MVP - Visual C++[^]

                    1 Reply Last reply
                    0
                    • T toxcct

                      of course, you're looking for devil every where :p a char* is not always a string ;)

                      char c = 'c';
                      char* pc = &c;

                      BYTE* pb1 = (BYTE*)pc;
                      BYTE* pb2 = reinterpret_cast<BYTE*>(pc);

                      BTW, looking at the level of the question, i think it's worth saying that a char IS a BYTE:

                      char c = 'c';

                      BYTE b = c;

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                      toxcct wrote:

                      of course, you're looking for devil every where [Poke tongue]

                      of course. :)

                      toxcct wrote:

                      BTW, looking at the level of the question, i think it's worth saying that a char IS a BYTE:

                      Nope. As you know (I know that you know ;) ), char is a signed integer ranging from -128 to 127, while a BYTE is an unsigned integer ranging from 0 to 255. ;P

                      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?

                      T 1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        toxcct wrote:

                        of course, you're looking for devil every where [Poke tongue]

                        of course. :)

                        toxcct wrote:

                        BTW, looking at the level of the question, i think it's worth saying that a char IS a BYTE:

                        Nope. As you know (I know that you know ;) ), char is a signed integer ranging from -128 to 127, while a BYTE is an unsigned integer ranging from 0 to 255. ;P

                        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]

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #15

                        but you're already going to far by interpreting the bits pattern. a BYTE is 8 bits, which is exactly what a char is supposed to be too. ;)

                        [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                        CPalliniC 1 Reply Last reply
                        0
                        • T toxcct

                          but you're already going to far by interpreting the bits pattern. a BYTE is 8 bits, which is exactly what a char is supposed to be too. ;)

                          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                          toxcct wrote:

                          but you're already going to far by interpreting the bits pattern.

                          Duty sir, duty. :rolleyes:

                          toxcct wrote:

                          a BYTE is 8 bits, which is exactly what a char is supposed to be too.

                          float and int (even pointers!) have the same size on 32 bit systems but we usually don't consider them being the same. :-D

                          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
                          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