Please how do I convert a string of hexidecimals “10B71356C845FB08E04951AAA2FF9F49” to integer? I tried using getBytes then Integer.valueOf but I got 32 integers instead of 16.

Any help would be greatly appreciated.

Thank you.

5
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by zeusprog

Can you give an example of a hex String and what int value you want to get from it?
An int holds 32 bits which can be represented with 8 hex digits. The example hex String you posted is longer than 8 digits and would not fit in an int.
The Integer class's parseInt() method can be used for conversion using the proper base.

Edited by NormR1

getBytes will extract the text bytes from the string - it doesn't know or care about hex. It will give you {'1', '0', 'B' ...
Then you convert each of those and you get 32 integer values each in the range 0-15.
To decode that as 16 integer values each in the range 0-255 you need to use substring to split the string into 16 strings each 2 chars long, then parse those.

Edited by JamesCherrill

Are these pairs of hex numbers, like 10 B7 etc..?

C program for hexadecimal to binary conversion

``````#include<stdio.h>
#define MAX 1000

int main(){
long int i=0;

printf("Enter any hexadecimal number: ");

printf("\nEquivalent binary value: ");
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1001"); break;
case 'A': printf("1010"); break;
case 'B': printf("1011"); break;
case 'C': printf("1100"); break;
case 'D': printf("1101"); break;
case 'E': printf("1110"); break;
case 'F': printf("1111"); break;
case 'a': printf("1010"); break;
case 'b': printf("1011"); break;
case 'c': printf("1100"); break;
case 'd': printf("1101"); break;
case 'e': printf("1110"); break;
case 'f': printf("1111"); break;
default:  printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
}
i++;
}

return 0;
}
``````

Sample output: