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. string to hex number

string to hex number

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++performance
6 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.
  • L Offline
    L Offline
    lune12
    wrote on last edited by
    #1

    Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:

    char* str = "23";
    char c;
    sscanf(str, "%x", &c);

    but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,

    G C S C C 5 Replies Last reply
    0
    • L lune12

      Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:

      char* str = "23";
      char c;
      sscanf(str, "%x", &c);

      but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,

      G Offline
      G Offline
      grassrootkit
      wrote on last edited by
      #2

      You could only mean two things. 1. Conver the number 23 to hex. 2. Take ascii value of every character, which is again a number and convert it to hex. You mean something other than these? If yes, you are wrong.

      1 Reply Last reply
      0
      • L lune12

        Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:

        char* str = "23";
        char c;
        sscanf(str, "%x", &c);

        but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Why don't you store it into an integer instead of a char ?

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • L lune12

          Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:

          char* str = "23";
          char c;
          sscanf(str, "%x", &c);

          but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          lune12 wrote:

          I have tried: char* str = "23"; char c; sscanf(str, "%x", &c); but the char c is 4 bytes in memory and this is not safe!!!

          That's because %x indicates you want to read an integer. Read it into an integer, bounds check it if you want, then assign it to the character. If you want to ensure you only read 2 hex characters from the input, use a width specification, like this:

          sscanf(str, "%2x", &int_value);

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          1 Reply Last reply
          0
          • L lune12

            Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:

            char* str = "23";
            char c;
            sscanf(str, "%x", &c);

            but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,

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

            you may use

            char * str = "23";
            char * endptr;
            char c = (char) strtol(str, &endptr, 16);

            :)

            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
            • L lune12

              Hi, I'm new in C++ and have a very basic question. suppose that I have a string such "23" how can I convert in to a byte 0x23 I have tried:

              char* str = "23";
              char c;
              sscanf(str, "%x", &c);

              but the char c is 4 bytes in memory and this is not safe!!! how can I do that? Thanks,

              C Offline
              C Offline
              cmk
              wrote on last edited by
              #6

              typedef unsigned char byte;
              typedef wchar_t charw;
              typedef const charw ccharw;

              ccharw *CharHexL (L"0123456789abcdef");

              byte HexToByte( charw C )
              {
              C = towlower(C);
              if( C >= CharHexL[ 0] && C <= CharHexL[ 9] ) return( byte(C-CharHexL[ 0]) );
              if( C >= CharHexL[10] && C <= CharHexL[15] ) return( byte(C-CharHexL[10]) + 10 );
              return(0);
              }

              inline byte HexToByte( charw C1, charw C2 )
              {
              return( (HexToByte(C1)<<4) + HexToByte(C2) );
              }

              If the string has an odd number of characters pass the first char to HexToByte(C) to get the values for the first byte. Walk the rest of the string two characters at a time using HexToByte(C1, C2) to convert the pairs to bytes.

              ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

              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