Java - Hex String representation to integer Hex - java

I have a string as "5F2A" as Hex. I would like to convert it as int 0x5F2A.
String str = "5F2A";
int number = someOperation(str);
And the number should be (with 0x)
0x5F2A
Is it possible?
To rephrase and share what I learnt today
Map<Integer, String> map = new HashMap<>();
map.put(0x5F2A, "somevalue");
System.out.println(map.get(24362));
System.out.println(map.get(0b0101111100101010));
Would give the value somevalue for both.

No transformation required:
System.out.println("0x" + str);
And to turn an arbitrary int into HEX representation:
Integer.toHexString(intNumber);
That should be all you need to get going!

int i = 0x5F2A not really means nothing because in memory, all is in binary, it's only when you print that it matters
String str = "5F2A";
int number = Integer.parseInt(str, 16); //alows to store an int, binary 0101111100101010
System.out.println(number); //24362 (decimal by default)
System.out.println(Integer.toHexString(number)); //5f2a (hexa possible too)
By default, it prints in (binary into) decimal format, but you can print in hexa format, but int i = 0x5F2A means at 100% the same as int i = 24362

See here
Integer.parseInt(/*your String*/, 16);
16 is the radix for hexadecimal.

Related

Convert hex string value to hex int

How can I convert the hexstring into the form of hexint below?
string hexstring = "0x67";
int hexint = 0x67;
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
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.
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.

Signed Hexadecimal to decimal in Java

I was wondering if it's possible to convert a signed Hexadecimal (negative) to its corresponding decimal value.
I assume that you have a hexadecimal value in form of a String.
The method parseInt(String s, int radix) can take a hexadecimal (signed) String and with the proper radix (16) it will parse it to an Integer.
int decimalInt = parseInt(hexaStr, 16);
the solution above only works if you have numbers like -FFAA07BB... if you want the Two's complements you'll have to convert it yourself.
String hex = "F0BDC0";
// First convert the Hex-number into a binary number:
String bin = Integer.toString(Integer.parseInt(hex, 16), 2);
// Now create the complement (make 1's to 0's and vice versa)
String binCompl = bin.replace('0', 'X').replace('1', '0').replace('X', '1');
// Now parse it back to an integer, add 1 and make it negative:
int result = (Integer.parseInt(binCompl, 2) + 1) * -1;
or if you feel like having a one-liner:
int result = (Integer.parseInt(Integer.toString(Integer.parseInt("F0BDC0", 16), 2).replace('0', 'X').replace('1', '0').replace('X', '1'), 2) + 1) * -1;
If the numbers get so big (or small), that an Integer will have an overflow, use Long.toString(...) and Long.parseLong(...) instead.

java - Enforce 4 digit hex representation of a binary number

Below is a snippet of my java code.
//converts a binary string to hexadecimal
public static String binaryToHex (String binaryNumber)
{
BigInteger temp = new BigInteger(binaryNumber, 2);
return temp.toString(16).toUpperCase();
}
If I input "0000 1001 0101 0111" (without the spaces) as my String binaryNumber, the return value is 957. But ideally what I want is 0957 instead of just 957. How do I make sure to pad with zeroes if hex number is not 4 digits?
Thanks.
You do one of the following:
Manually pad with zeroes
Use String.format()
Manually pad with zeroes
Since you want extra leading zeroes when shorter than 4 digits, use this:
BigInteger temp = new BigInteger(binaryNumber, 2);
String hex = temp.toString(16).toUpperCase();
if (hex.length() < 4)
hex = "000".substring(hex.length() - 1) + hex;
return hex;
Use String.format()
BigInteger temp = new BigInteger(binaryNumber, 2);
return String.format("%04X", temp);
Note, if you're only expecting the value to be 4 hex digits long, then a regular int can hold the value. No need to use BigInteger.
In Java 8, do it by parsing the binary input as an unsigned number:
int temp = Integer.parseUnsignedInt(binaryNumber, 2);
return String.format("%04X", temp);
The BigInteger becomes an internal machine representation of whatever value you passed in as a String in binary format. Therefore, the machine does not know how many leading zeros you would like in the output format. Unfortunately, the method toString in BigInteger does not allow any kind of formatting, so you would have to do it manually if you attempt to use the code you showed.
I customized your code a bit to include leading zeros based on input string:
public static void main(String[] args) {
System.out.println(binaryToHex("0000100101010111"));
}
public static String binaryToHex(String binaryNumber) {
BigInteger temp = new BigInteger(binaryNumber, 2);
String hexStr = temp.toString(16).toUpperCase();
int b16inLen = binaryNumber.length()/4;
int b16outLen = hexStr.length();
int b16padding = b16inLen - b16outLen;
for (int i=0; i<b16padding; i++) {
hexStr=('0'+hexStr);
}
return hexStr;
}
Notice that the above solution counts up the base16 digits in the input and calculates the difference with the base16 digits in the output. So, it requires the user to input a full '0000' to be counted up. That is '000 1111' will be displayed as 'F' while '0000 1111' as '0F'.

Java Hex number increase by one

I have some auto generated ids represented as a HEX String. I want to find the next 1000 values. For instance, let's suppose I have the following string
String keyFrom = "536a11dae4b062cab536549d";
How can I get from a java code the following, into String?
536a11dae4b062cab536549e
536a11dae4b062cab536549f
536a11dae4b062cab53654a0
536a11dae4b062cab53654a1
536a11dae4b062cab53654a2 ... etc.
Use BigInteger as below
BigInteger decimal = new BigInteger("536a11dae4b062cab536549d",16);
for ( int i=0;i<1000;i++){
decimal = decimal.add(BigInteger.ONE);
System.out.println(decimal.toString(16));
}
Convert your String to BigInteger and increment it:
BigInteger bigInt = new BigInteger(hexString, 16);
for(int i = 0 ; i < 1000 ; ++i) {
// do something with bigInt...
System.out.println(bigInt.toString(16));
bigInt = bigInt.add(BigInteger.ONE);
}
EDIT: If your using hex strings longer than ~8 characters, use the solution using BigInteger above.
Use Integer#parseInt(String, 16) to parse the hex string into an integer, add one to it, and then use Integer#toHexString to turn it back to hexadecimal.
String hexString = "A953CF";
// 16 sepcifies the string to be in base 16, hexadecimal
int hexAsInt = Integer.parseInt(hexString, 16);
hexAsInt += 6; // Add 6
String newHexString = Integer.toHexString(hexAsInt);
System.out.println(newHexString);
--> A953D4
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String)
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toHexString(int)

Increment a Hex value (JAVA)

can you increment a hex value in Java? i.e. "hex value" = "hex value"++
It depends how the hex value is stored. If you've got the hex value in a string, convert it to an Integer, increment and convert it back.
int value = Integer.parseInt(hex, 16);
value++;
String incHex = Integer.toHexString(value);
Short answer: yes. It's
myHexValue++;
Longer answer: It's likely your 'hex value' is stored as an integer. The business of converting it into a hexadecimal (as opposed to the usual decimal) string is done with
Integer.toHexString( myHexValue )
and from a hex string with
Integer.parseInt( someHexString, 16 );
M.
What do you mean with "hex value"? In what data type is your value stored?
Note that int/short/char/... don't care how your value is represented initially:
int i1 = 0x10;
int i2 = 16;
i1 and i2 will have the exact same content. Java (and most other languages as well) don't care about the notation of your constants/values.
Yes. All ints are binary anyway, so it doesn't matter how you declare them.
int hex = 0xff;
hex++; // hex is now 0x100, or 256
int hex2 = 255;
hex2++; // hex2 is now 256, or 0x100
The base of the number is purely a UI issue. Internally an integer is stored as binary. Only when you convert it to human representation do you choose a numeric base. So you're question really boils down to "how to increment an integer?".

Categories