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. Cleanest way to convert a two character string like "4D" to it's hex-equivalent-number ?

Cleanest way to convert a two character string like "4D" to it's hex-equivalent-number ?

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 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.
  • O Offline
    O Offline
    ohadp
    wrote on last edited by
    #1

    I currently do: _stscanf(string, L"%x", &number); it doesn't seem like any of the atoi/l functions can handle hexa-decimal input, any suggestions for something cleaner, or is as clean as it gets ? :-)

    T J 2 Replies Last reply
    0
    • O ohadp

      I currently do: _stscanf(string, L"%x", &number); it doesn't seem like any of the atoi/l functions can handle hexa-decimal input, any suggestions for something cleaner, or is as clean as it gets ? :-)

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

      do this :

      char str[2]; // stored with your two characters
      unsigned char ucConvertedNumber,
      MSB, // most significant bits
      LSB; // less significant bits

      LSB = ((str[1] >= '0') && (str[1] <= '9')) ? str[1] - '0' :
      ((str[1] >= 'a') && (str[1] <= 'f')) ? str[1] - 'a' + 10 :
      ((str[1] >= 'A') && (str[1] <= 'F')) ? str[1] - 'A' + 10 : -1; // this happens only if your string is not a correct hexa representation
      // do some error checking here...

      MSB = ((str[0] >= '0') && (str[0] <= '9')) ? str[0] - '0' :
      ((str[0] >= 'a') && (str[0] <= 'f')) ? str[0] - 'a' + 10 :
      ((str[0] >= 'A') && (str[0] <= 'F')) ? str[0] - 'A' + 10 : -1; // this happens only if your string is not a correct hexa representation
      // do some error checking here...

      MSB = MSB << 4;

      ucConvertedNumber = MSB | LSB;

      you've got it... you can nox do :

      printf("Decimal form :%d\n"
      "Hexadecimal form :%x\n",
      ucConvertedNumber,
      ucConvertedNumber);


      TOXCCT >>> GEII power

      1 Reply Last reply
      0
      • O ohadp

        I currently do: _stscanf(string, L"%x", &number); it doesn't seem like any of the atoi/l functions can handle hexa-decimal input, any suggestions for something cleaner, or is as clean as it gets ? :-)

        J Offline
        J Offline
        jmkhael
        wrote on last edited by
        #3

        long strtol( const char *nptr, char **endptr, int base ); acString strtol(acString, NULL, 16); Papa while (TRUE) Papa.WillLove ( Bebe ) ;

        O 1 Reply Last reply
        0
        • J jmkhael

          long strtol( const char *nptr, char **endptr, int base ); acString strtol(acString, NULL, 16); Papa while (TRUE) Papa.WillLove ( Bebe ) ;

          O Offline
          O Offline
          ohadp
          wrote on last edited by
          #4

          this is the function i was looking for :-)

          T 1 Reply Last reply
          0
          • O ohadp

            this is the function i was looking for :-)

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

            ok for a function, but understand what it does because it isn't a standard function...


            TOXCCT >>> GEII power

            P 1 Reply Last reply
            0
            • T toxcct

              ok for a function, but understand what it does because it isn't a standard function...


              TOXCCT >>> GEII power

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              Nice joke.


              God is Real, unless declared Integer.

              T 1 Reply Last reply
              0
              • P Prakash Nadar

                Nice joke.


                God is Real, unless declared Integer.

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

                ok, i was thinking of another. just verified.


                TOXCCT >>> GEII power

                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