Hex to Decimal conversion in C

Posted by darkie15 on Stack Overflow See other posts from Stack Overflow or by darkie15
Published on 2010-04-04T22:27:19Z Indexed on 2010/04/04 22:33 UTC
Read the original article Hit count: 347

Filed under:
|
|

Hi All,

Here is my code which is doing the conversion from hex to decimal. The hex values are stored in a unsigned char array:

  int liIndex ;
  long hexToDec ;
  unsigned char length[4];

  for (liIndex = 0; liIndex < 4 ; liIndex++)
  {
       length[liIndex]= (unsigned char) *content;
       printf("\n Hex value is %.2x", length[liIndex]);
       content++;
  }
  hexToDec = strtol(length, NULL, 16);

Each array element contains 1 byte of information and I have read 4 bytes. When I execute it, here is the output that I get :

 Hex value is 00
 Hex value is 00
 Hex value is 00
 Hex value is 01
 Chunk length is 0

Can any one please help me understand the error here. Th decimal value should have come out as 1 instead of 0.

Regards, darkie

© Stack Overflow or respective owner

Related posts about hexadecimal

Related posts about decimal