Home > database >  Convert hex string value to hex int
Convert hex string value to hex int

Time:10-24

How can I convert the hexstring into the form of hexint below?

string hexstring = "0x67";
int hexint = 0x67;

CodePudding user response:

Integer#decode can be used to convert hexadecmial string representation into its integer value:

Integer.decode("0x67");

This function automatically detects the correct base and will parse return the int 103 (0x67 = 6*16 7). If you want to manually specify a different base, see my other answer

CodePudding user response:

If you only have a single byte, you can strip of the leading "0x" part and then parse as a base-16 number with Integer#parseInt:

Integer.parseInt("0x67".substring(2), 0x10);
Integer.parseInt("0x67".substring(2), 16);

0x10 is the hexadecimal representation of the decimal number 16.

CodePudding user response:

String hexstring = "67";
int hexint = Integer.parseInt(hexstring, 16);
System.out.println(hexint); // 103 (decimal)

With Integer.parseInt(String s, int radix) you can parse a String to an int.

The radix is the base of that number system, in case of hex-values it is 16.

  •  Tags:  
  • java
  • Related