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. how can i convert a BYTE value into CString

how can i convert a BYTE value into CString

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 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.
  • R Offline
    R Offline
    rajneshmalik
    wrote on last edited by
    #1

    hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong

    F N L 3 Replies Last reply
    0
    • R rajneshmalik

      hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong

      F Offline
      F Offline
      Florin Crisan
      wrote on last edited by
      #2

      Assuming that you want to create a string representation of the number (that is 32 => "32" rather than 32 => " " – space being the character with the ASCII code 32), you can use something like this: CString x; BYTE b = 123; x.Format("%u", (unsigned int) b); // "123" or x.Format("%x", (unsigned int) b); // "7b" You need to convert it to unsigned int because there doesn't seem to be a way to specify a 1-byte integer in the format string. Or, if you feel adventurous, you can try stringstream[^] or The Boost Format Library[^].

      Florin Crisan

      F 1 Reply Last reply
      0
      • R rajneshmalik

        hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong

        N Offline
        N Offline
        Nishad S
        wrote on last edited by
        #3

        You can use CString::Format csValue.Format( "%x", bByte[1] );

        - NS - [ODBaseBtn]

        modified on Monday, December 31, 2007 9:36:06 AM

        1 Reply Last reply
        0
        • R rajneshmalik

          hi to all itook a varible BYTE *bByte and want to convert bByte[1] into a CString type varible,bByte varible has hex value, i done CString csValue=(CHAR)bByte; where am i wrong

          L Offline
          L Offline
          Lea Hayes
          wrote on last edited by
          #4

          Hi, You are attempting to cast a BYTE* into a CHAR. A BYTE* is a pointer to BYTE data....so the casted CHAR will be a CHAR representation of the pointer address of bByte. CString csValue = (CHAR)bByte[0]; // for individual character or CString csValue = (CHAR*)bByte; // for entire byte array // be sure the byte array has a null terminator character '\0' at the end! If you want to show the hex value of the byte, you can do something like the following: // for numerical value in hex i.e. "0A" if bByte[0] == 0x0A. CString csValue; csValue.Format(_T("%02X"), bByte[0]); If you wanted to show the entire contents of the byte array as hex then you will need to wrap the above within a loop and concatenate the result each time to the result string. // For example, the byte array { 0x0A, 0x0B, 0x12 } // will would create the string "0A0B12". CString csValue, csTemp; BYTE *pActiveByte = bByte; while(*pActiveByte) { csTemp.Format(_T("%02X"), *pActiveByte++); csValue.Append(csTemp); } I hope that this helps... Lea Hayes

          F 1 Reply Last reply
          0
          • F Florin Crisan

            Assuming that you want to create a string representation of the number (that is 32 => "32" rather than 32 => " " – space being the character with the ASCII code 32), you can use something like this: CString x; BYTE b = 123; x.Format("%u", (unsigned int) b); // "123" or x.Format("%x", (unsigned int) b); // "7b" You need to convert it to unsigned int because there doesn't seem to be a way to specify a 1-byte integer in the format string. Or, if you feel adventurous, you can try stringstream[^] or The Boost Format Library[^].

            Florin Crisan

            F Offline
            F Offline
            Florin Crisan
            wrote on last edited by
            #5

            In your code, bByte is actually a pointer to a BYTE rather than a BYTE. You should have named it better: pbySomethingUseful. p stands for pointer, by stands for byte (b stands for bool, and that one is actually as big as an int). The rest should be a useful name :) So your code should look like: csValue.Format("%x", (unsigned int) (*pbySomethingUseful)).

            Florin Crisan

            1 Reply Last reply
            0
            • L Lea Hayes

              Hi, You are attempting to cast a BYTE* into a CHAR. A BYTE* is a pointer to BYTE data....so the casted CHAR will be a CHAR representation of the pointer address of bByte. CString csValue = (CHAR)bByte[0]; // for individual character or CString csValue = (CHAR*)bByte; // for entire byte array // be sure the byte array has a null terminator character '\0' at the end! If you want to show the hex value of the byte, you can do something like the following: // for numerical value in hex i.e. "0A" if bByte[0] == 0x0A. CString csValue; csValue.Format(_T("%02X"), bByte[0]); If you wanted to show the entire contents of the byte array as hex then you will need to wrap the above within a loop and concatenate the result each time to the result string. // For example, the byte array { 0x0A, 0x0B, 0x12 } // will would create the string "0A0B12". CString csValue, csTemp; BYTE *pActiveByte = bByte; while(*pActiveByte) { csTemp.Format(_T("%02X"), *pActiveByte++); csValue.Append(csTemp); } I hope that this helps... Lea Hayes

              F Offline
              F Offline
              Florin Crisan
              wrote on last edited by
              #6

              I must say that I am little surprised that it works with a BYTE. Weren't %x and %u supposed to take a (four-byte) integer?

              Florin Crisan

              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