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 to convert hex to string?

How to convert hex to string?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialsysadmindata-structuresquestion
11 Posts 4 Posters 1 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
    Rostfrei
    wrote on last edited by
    #1

    Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like: string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); } This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". Rostfrei

    O S G 4 Replies Last reply
    0
    • R Rostfrei

      Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like: string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); } This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". Rostfrei

      O Offline
      O Offline
      Owner drawn
      wrote on last edited by
      #2

      Rostfrei wrote:

      char a[3] = {0};

      You have to increase the size of the array(may be something like a[10], this will be much safer).

      Jesus Lives Forever - Amen:rose:

      --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

      R 1 Reply Last reply
      0
      • O Owner drawn

        Rostfrei wrote:

        char a[3] = {0};

        You have to increase the size of the array(may be something like a[10], this will be much safer).

        Jesus Lives Forever - Amen:rose:

        --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

        R Offline
        R Offline
        Rostfrei
        wrote on last edited by
        #3

        I tried that already, but it doesn't work. Rostfrei

        O 1 Reply Last reply
        0
        • R Rostfrei

          I tried that already, but it doesn't work. Rostfrei

          O Offline
          O Offline
          Owner drawn
          wrote on last edited by
          #4

          7f ff 1a. I am getting this as output.

          Jesus Lives Forever - Amen:rose:

          --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

          R 1 Reply Last reply
          0
          • O Owner drawn

            7f ff 1a. I am getting this as output.

            Jesus Lives Forever - Amen:rose:

            --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

            R Offline
            R Offline
            Rostfrei
            wrote on last edited by
            #5

            Yes and it should be 7f 80 1a Rostfrei

            1 Reply Last reply
            0
            • R Rostfrei

              Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like: string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); } This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". Rostfrei

              O Offline
              O Offline
              Owner drawn
              wrote on last edited by
              #6

              Rostfrei wrote:

              char buffer2[100] = {0x7f, 0x80, 0x1a};

              Here is the problem. Make that unsigned char buffer2[100];

              Jesus Lives Forever - Amen:rose:

              --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

              R 1 Reply Last reply
              0
              • O Owner drawn

                Rostfrei wrote:

                char buffer2[100] = {0x7f, 0x80, 0x1a};

                Here is the problem. Make that unsigned char buffer2[100];

                Jesus Lives Forever - Amen:rose:

                --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

                R Offline
                R Offline
                Rostfrei
                wrote on last edited by
                #7

                Yes! It works now! Thank you! Rostfrei

                1 Reply Last reply
                0
                • R Rostfrei

                  Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like: string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); } This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". Rostfrei

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  In my oppinion there is way too much printfing in C++ code. Why not something like this? #include <sstream> #include <iomanip>   string print_buff; BYTE buffer2[100] = {0x7f, 0x80, 0x1a};   for(int i = 0; i < 3; i++) {      ostringstream ss;      ss << setw(2) << setfill('0') << hex << buffer2[i];      print_buff += ss.str(); } Steve

                  O R 2 Replies Last reply
                  0
                  • S Stephen Hewitt

                    In my oppinion there is way too much printfing in C++ code. Why not something like this? #include <sstream> #include <iomanip>   string print_buff; BYTE buffer2[100] = {0x7f, 0x80, 0x1a};   for(int i = 0; i < 3; i++) {      ostringstream ss;      ss << setw(2) << setfill('0') << hex << buffer2[i];      print_buff += ss.str(); } Steve

                    O Offline
                    O Offline
                    Owner drawn
                    wrote on last edited by
                    #9

                    Game over ;P :jig:

                    Jesus Lives Forever - Amen:rose:

                    --Owner drawn:rose: --An eye for an eye makes the whole world blind. --Jesus is Lord:rose:

                    1 Reply Last reply
                    0
                    • R Rostfrei

                      Hello! I have some char array of hexadecimal values I get from the network. I want to convert them to some printable form. For example I did it like: string print_buff; char buffer2[100] = {0x7f, 0x80, 0x1a}; for(int i = 0; i < 3; i++){ char a[3] = {0}; sprintf(a, "%2.0x", buffer2[i]); print_buff.append(a, 2); } This works well to the values 0x7f (127), but for the numbers bigger than 0x7f it doesn't work properly anymore. If I look at 'a[3]' character array (in debuger) I can see that 0x80 has converted to "ffffff80" string. How is this even possible when I limited string with formating option to 2? When I append characters afterwards, "ff" is appended not "80". Rostfrei

                      G Offline
                      G Offline
                      Gary R Wheeler
                      wrote on last edited by
                      #10

                      In most C++ compilers, char is a signed data type. This means that values greater than or equal to 0x80 interpret the high bit as a sign bit. You are using an integer format %2.0x, so it extends the sign bit. Try this:

                      string print_buff;

                      char buffer2[100] = {0x7f, 0x80, 0x1a};

                      for(int i = 0; i < 3; i++){
                      char a[3] = {0};
                      sprintf(a, "%02x", **(unsigned)**buffer2[i]);
                      print_buff.append(a, 2);
                      }

                      The (unsigned) cast converts the character to an unsigned int. I also changed the format slightly.


                      Software Zen: delete this;

                      Fold With Us![^]

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        In my oppinion there is way too much printfing in C++ code. Why not something like this? #include <sstream> #include <iomanip>   string print_buff; BYTE buffer2[100] = {0x7f, 0x80, 0x1a};   for(int i = 0; i < 3; i++) {      ostringstream ss;      ss << setw(2) << setfill('0') << hex << buffer2[i];      print_buff += ss.str(); } Steve

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

                        Hello! By looking at the code I know it should work, but it just doesn't and I don't know why. I have an array of hexadecimal numbers (char) 00 1C 61 1A 80 02 07 80 A1 08 06 06 2B 0C 00 81 34 01 A2 03 02 01 00 A3 05 A1 03 02 01 01 when I try to convert it to string the result in print_buff is only "0". Rostfrei

                        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