how to convert byte array with hex number to equivalent decimal value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mapel
    New Member
    • Sep 2012
    • 6

    how to convert byte array with hex number to equivalent decimal value

    Hi,
    I tried to convert byte array with hex to equivalent decimal value as follows but it gives me unexpected results:

    byte hex_arr[] = { 0x00, 0x01, 0xab, 0x90};
    unsigned long i=0;
    i = hex_arr[3] + (hex_arr[2] << 8) + (hex_arr[1] << 16) + (hex_arr[0] << 24);
    the output is 4294945680
    Correct output should be 109456

    but when I try with byte hex_arr[]={0x00,0x00,0x0 f,0xff};
    it gives correct out put 4095

    the correct output works until the hex values is {0x00,0x00,0x7f ,0xff}

    Any help is appreciated.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Is byte defined to a sign or unsigned value. It looks like you are getting a sign extension error which suggests byte is signed.

    Personally I tend to use | (bitwise or) rather than + (addition) when combining values in this sort of algorithm.

    Comment

    • Mapel
      New Member
      • Sep 2012
      • 6

      #3
      Hi Banfa,

      I tried use bitwise or..but the same problem..

      byte hex_arr[] = { 0x00, 0x01, 0xab, 0x90};
      unsigned long i=0;

      i = hex_arr[3];
      i |= hex_arr[2] << 8;
      i |= hex_arr[1] << 16;
      i |= hex_arr[0] << 24;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I may not have had enough coffee this morning but isn't a hex 10 an A? Isn't A 65? So how does a conversion work using a shift? I have always used a switch where case A got converted to 10.

        Comment

        • Mapel
          New Member
          • Sep 2012
          • 6

          #5
          Hi weaknessforcats ,

          A is for 10.

          Thanks for the attention.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            @weaknessforcat s this is not converting ASCII digits to their binary value but rather combining binary bytes together to form an integer with a larger number of bits as you might do if you received the data from some byte stream (network or serial line for example).

            @Mapel the suggestion to use | instead of + was not meant to fix your problem, it is a little more efficient and logically more correct for the operation you are trying to do. I still believe the issue is sign extension of a signed integer value so I ask again what is the type byte defined to be.

            Comment

            • Mapel
              New Member
              • Sep 2012
              • 6

              #7
              @Banfa I am not sure of what sign extension but when I try to print the output(with signed int variable) it gives negative value. I think your point is correct. The problem is from sign extension but how can I fix this problem?
              Thanks.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Normally by making sure that you are using unsigned types, being unsigned sign extension doesn't apply to them which is why I keep asking "what is the defined type of byte?" which it would be quite nice to get an answer to.

                Something else you many wish to try is casting your bytes to the correct type before you shift them i.e. (((unsigned long)hex_arr[0]) << 24)

                Comment

                • Mapel
                  New Member
                  • Sep 2012
                  • 6

                  #9
                  Thanks so much Banfa. I got it.

                  Comment

                  Working...