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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Converting unsigned short int to its hex representation

Converting unsigned short int to its hex representation

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
7 Posts 5 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.
  • Y Offline
    Y Offline
    yeah1000
    wrote on last edited by
    #1

    Hello, i ran into a bit of trouble when converting unsigned short int to its hex string representation. What i need is a two byte representation with a space between the bytes (e.g "0f 43" for 3907 of "00 ff" for 255) The solution i came up with is the following: unsigned char* CreateHexValues(unsigned short int inValue) { char Hex[5] = {0, 0, 0, 0, 0}; _itoa_s(inValue, Hex, 16); unsigned char *hexOut = new unsigned char[5]; hexOut[2] = ' '; if(inValue <= 15) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = '0'; hexOut[4] = Hex[0]; } else if(inValue > 15 && inValue <= 255) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = Hex[0]; hexOut[4] = Hex[1]; } else if(inValue > 255 && inValue <= 4095) { hexOut[0] = '0'; hexOut[1] = Hex[0]; hexOut[3] = Hex[1]; hexOut[4] = Hex[2]; } else if (inValue > 4096) { hexOut[0] = Hex[0]; hexOut[1] = Hex[1]; hexOut[3] = Hex[2]; hexOut[4] = Hex[3]; } return hexOut; } So my question is: is there a better/quicker way to do that? The problem is that when i convert it with _itoa_s it could be in the following formats: f43, 43, 3 (depending on inValue) without any zeros in front of it. Another problem is with the Hex[5] array, my inValue can never be bigger than 65535, but for some reason i get an assertion failure when i change its size to Hex[4] which should be enough for 65535. Why could that be? Thanks for all replies in advance

    L CPalliniC Y S 4 Replies Last reply
    0
    • Y yeah1000

      Hello, i ran into a bit of trouble when converting unsigned short int to its hex string representation. What i need is a two byte representation with a space between the bytes (e.g "0f 43" for 3907 of "00 ff" for 255) The solution i came up with is the following: unsigned char* CreateHexValues(unsigned short int inValue) { char Hex[5] = {0, 0, 0, 0, 0}; _itoa_s(inValue, Hex, 16); unsigned char *hexOut = new unsigned char[5]; hexOut[2] = ' '; if(inValue <= 15) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = '0'; hexOut[4] = Hex[0]; } else if(inValue > 15 && inValue <= 255) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = Hex[0]; hexOut[4] = Hex[1]; } else if(inValue > 255 && inValue <= 4095) { hexOut[0] = '0'; hexOut[1] = Hex[0]; hexOut[3] = Hex[1]; hexOut[4] = Hex[2]; } else if (inValue > 4096) { hexOut[0] = Hex[0]; hexOut[1] = Hex[1]; hexOut[3] = Hex[2]; hexOut[4] = Hex[3]; } return hexOut; } So my question is: is there a better/quicker way to do that? The problem is that when i convert it with _itoa_s it could be in the following formats: f43, 43, 3 (depending on inValue) without any zeros in front of it. Another problem is with the Hex[5] array, my inValue can never be bigger than 65535, but for some reason i get an assertion failure when i change its size to Hex[4] which should be enough for 65535. Why could that be? Thanks for all replies in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      A simpler way is to use the one of the xprintf() type functions. Take a look here[^] at the x and X format specifiers.

      1 Reply Last reply
      0
      • Y yeah1000

        Hello, i ran into a bit of trouble when converting unsigned short int to its hex string representation. What i need is a two byte representation with a space between the bytes (e.g "0f 43" for 3907 of "00 ff" for 255) The solution i came up with is the following: unsigned char* CreateHexValues(unsigned short int inValue) { char Hex[5] = {0, 0, 0, 0, 0}; _itoa_s(inValue, Hex, 16); unsigned char *hexOut = new unsigned char[5]; hexOut[2] = ' '; if(inValue <= 15) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = '0'; hexOut[4] = Hex[0]; } else if(inValue > 15 && inValue <= 255) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = Hex[0]; hexOut[4] = Hex[1]; } else if(inValue > 255 && inValue <= 4095) { hexOut[0] = '0'; hexOut[1] = Hex[0]; hexOut[3] = Hex[1]; hexOut[4] = Hex[2]; } else if (inValue > 4096) { hexOut[0] = Hex[0]; hexOut[1] = Hex[1]; hexOut[3] = Hex[2]; hexOut[4] = Hex[3]; } return hexOut; } So my question is: is there a better/quicker way to do that? The problem is that when i convert it with _itoa_s it could be in the following formats: f43, 43, 3 (depending on inValue) without any zeros in front of it. Another problem is with the Hex[5] array, my inValue can never be bigger than 65535, but for some reason i get an assertion failure when i change its size to Hex[4] which should be enough for 65535. Why could that be? Thanks for all replies in advance

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

        I would do

        void CreateHexValues(unsigned short inValue, char hexOut[6])
        {
        sprintf(hexOut, "%02x %02x", (inValue >> 8), (inValue & 0xFF) );
        }

        :) Please note: Array allocation is, by design...:rolleyes:, responsibility of the caller.

        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]

        modified on Tuesday, November 3, 2009 9:27 AM

        In testa che avete, signor di Ceprano?

        D 1 Reply Last reply
        0
        • CPalliniC CPallini

          I would do

          void CreateHexValues(unsigned short inValue, char hexOut[6])
          {
          sprintf(hexOut, "%02x %02x", (inValue >> 8), (inValue & 0xFF) );
          }

          :) Please note: Array allocation is, by design...:rolleyes:, responsibility of the caller.

          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]

          modified on Tuesday, November 3, 2009 9:27 AM

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          CPallini wrote:

          void CreateHexValues(unsigned short inValue, char hexOut[5])

          Shouldn't it be hexOut[6] to make room for the \0 character?

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          CPalliniC 1 Reply Last reply
          0
          • D David Crow

            CPallini wrote:

            void CreateHexValues(unsigned short inValue, char hexOut[5])

            Shouldn't it be hexOut[6] to make room for the \0 character?

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

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

            Definitely! :-O Fixed: thank you! :) BTW: Shhhhhhhhhhhhhhhhhhhhh, don't tell Rajesh! :rolleyes:

            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
            • Y yeah1000

              Hello, i ran into a bit of trouble when converting unsigned short int to its hex string representation. What i need is a two byte representation with a space between the bytes (e.g "0f 43" for 3907 of "00 ff" for 255) The solution i came up with is the following: unsigned char* CreateHexValues(unsigned short int inValue) { char Hex[5] = {0, 0, 0, 0, 0}; _itoa_s(inValue, Hex, 16); unsigned char *hexOut = new unsigned char[5]; hexOut[2] = ' '; if(inValue <= 15) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = '0'; hexOut[4] = Hex[0]; } else if(inValue > 15 && inValue <= 255) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = Hex[0]; hexOut[4] = Hex[1]; } else if(inValue > 255 && inValue <= 4095) { hexOut[0] = '0'; hexOut[1] = Hex[0]; hexOut[3] = Hex[1]; hexOut[4] = Hex[2]; } else if (inValue > 4096) { hexOut[0] = Hex[0]; hexOut[1] = Hex[1]; hexOut[3] = Hex[2]; hexOut[4] = Hex[3]; } return hexOut; } So my question is: is there a better/quicker way to do that? The problem is that when i convert it with _itoa_s it could be in the following formats: f43, 43, 3 (depending on inValue) without any zeros in front of it. Another problem is with the Hex[5] array, my inValue can never be bigger than 65535, but for some reason i get an assertion failure when i change its size to Hex[4] which should be enough for 65535. Why could that be? Thanks for all replies in advance

              Y Offline
              Y Offline
              yeah1000
              wrote on last edited by
              #6

              Thank you very much, brilliant solution ;)

              1 Reply Last reply
              0
              • Y yeah1000

                Hello, i ran into a bit of trouble when converting unsigned short int to its hex string representation. What i need is a two byte representation with a space between the bytes (e.g "0f 43" for 3907 of "00 ff" for 255) The solution i came up with is the following: unsigned char* CreateHexValues(unsigned short int inValue) { char Hex[5] = {0, 0, 0, 0, 0}; _itoa_s(inValue, Hex, 16); unsigned char *hexOut = new unsigned char[5]; hexOut[2] = ' '; if(inValue <= 15) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = '0'; hexOut[4] = Hex[0]; } else if(inValue > 15 && inValue <= 255) { hexOut[0] = '0'; hexOut[1] = '0'; hexOut[3] = Hex[0]; hexOut[4] = Hex[1]; } else if(inValue > 255 && inValue <= 4095) { hexOut[0] = '0'; hexOut[1] = Hex[0]; hexOut[3] = Hex[1]; hexOut[4] = Hex[2]; } else if (inValue > 4096) { hexOut[0] = Hex[0]; hexOut[1] = Hex[1]; hexOut[3] = Hex[2]; hexOut[4] = Hex[3]; } return hexOut; } So my question is: is there a better/quicker way to do that? The problem is that when i convert it with _itoa_s it could be in the following formats: f43, 43, 3 (depending on inValue) without any zeros in front of it. Another problem is with the Hex[5] array, my inValue can never be bigger than 65535, but for some reason i get an assertion failure when i change its size to Hex[4] which should be enough for 65535. Why could that be? Thanks for all replies in advance

                S Offline
                S Offline
                softwaremonkey
                wrote on last edited by
                #7

                How about something like:

                char szBuffer[10];
                short value = 1234;
                sprintf(szBuffer, "%02X %02X", HIBYTE(value), LOBYTE(value));

                Tony

                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