Decimal String to hexadecimal String - java

I have a String encoded in this kind of format:
223175087923687075112234402528973166755
The decoded string looks like:
a7e5f55e1dbb48b799268e1a6d8618a3
I need to convert from Decimal to Hexadecimal, but the input number is much bigger than the int or long types can handle, so how can I convert this?

You can use BigInteger :
BigInteger big = new BigInteger("223175087923687075112234402528973166755");
System.out.println(big.toString(16));
Output :
a7e5f55e1dbb48b799268e1a6d8618a3

Related

How to Convert UTF-16 Surrogate Decimal to UNICODE in Java

I have some string data like
&#55357 ;&#56842 ;
These are surrogate pairs in UTF 16 in decimal format.
How can I convert them to Unicode Code Points in Java, so that my client can understand the Unicode decimal html entity without the surrogate pair?
Example: &#128522 ; - Get this response for the above string
Assuming you already parsed the string to get the 2 numbers, just create a String from those two char values:
String s = new String(new char[] { 55357, 56842 });
System.out.println(s);
Output
😊
To get the code point of that:
s.codePointAt(0) // returns 128522
You don't have to create a string though:
Character.toCodePoint((char) 55357, (char) 56842) // returns 128522

Convert a string of bits to unicode character in java

I'm trying to convert a string of bits into Unicode characters in java. Problem is that I only get chines signs etc.
String bits = "01010011011011100110000101110010"
Anyone know how to do this?
Values <= 32bits
Use Integer.parseInt to parse the binary string, then convert it to byte array (using ByteBuffer) and finally convert byte array to String:
String bits = "01010011011011100110000101110010"
new String(
ByteBuffer.allocate(4).putInt(
Integer.parseInt(bits, 2)
).array(),
StandardCharsets.UTF_8
);
Values > 32bits
For arbitrary large bits String you can use also BigInteger:
new String(
new BigInteger(bits, 2).toByteArray(),
StandardCharsets.UTF_8
);
Result
Snar

convert hexadecimal number to binary

int hex=Integer.parseInt(str.trim(),16);
String binary=Integer.toBinaryString(hex);
i have a array of hexadecimal numbers as strings and i want to convert those numbers to binary string, above is the code i used and in there, i get a error as shown below
Exception in thread "main" java.lang.NumberFormatException: For input string: "e24dd004"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at sew1.javascript.main(javascript.java:20)
Maximum Integer in Java is 0x7fffffff, because it is signed.
Use
Long.parseLong(str.trim(),16);
or
BigInteger(str.trim(),16);
instead.
The problem is that e24dd004 is larger than int can handle in Java. If you use long, it will be fine:
String str = "e24dd004";
long hex = Long.parseLong(str.trim(),16);
String binary=Long.toBinaryString(hex);
System.out.println(binary);
That will be valid for hex up to 7fffffffffffffff.
An alternative, however, would be to do a direct conversion of each hex digit to 4 binary digits, without ever converting to a numeric value. One simple way of doing that would be to have a Map<Character, String> where each string is 4 digits. That will potentially leave you with leading 0s of course.
Use BigInteger as below:
BigInteger bigInteger = new BigInteger("e24dd004", 16);
String binary = bigInteger.toString(2);
Or using Long.toBinaryString() as below:
long longs = Long.parseLong("e24dd004",16);
String binary = Long.toBinaryString(longs);
Since java-8, you can treat integers as unsigned, so you could do:
String str = "e24dd004";
int i = Integer.parseUnsignedInt(str, 16);
String binary = Integer.toBinaryString(i); //11100010010011011101000000000100
String backToHex = Integer.toUnsignedString(i, 16); //e24dd004
You would be able to handle values that are not larger than 2^32-1 (instead of 2^31-1 if you use signed values).
If you can't use it, you'll have to parse it as a long like other answers showed.

Java: Convert a hexadecimal encoded String to a hexadecimal byte

An Item-ID in hexadecimal and the amount in decimal has to be entered in two JTextFields.
Now I have to convert the Item ID hexadecimal encoded in a String to a byte hexadecimal.
String str = itemIdField.getText(); // Would be, for example, "5e"
byte b = // Should be 0x5e then.
So if str = "5e", b = 0x5e
if str = "6b" b = 0x6b and so on.
Does anybody now, what the code to convert that would be then?
Google doesn't know, it thinks, I want to convert the text to a byte[]
Thank you, Richie
You can use Byte.parseByte(str, 16), that will return the byte value represented by the hexadecimal value in str.

How to convert a string representation of unicode hex "0x20000" to the int code point 0x20000 in Java

I have a list of String representations of unicode hex values such as "0x20000" (π €€) and "0x00F8" (ΓΈ) that I need to get the int code point of so that I can use functions such as:
char[] chars = Character.toChars(0x20000);
This should cover the BMP as well as supplementary characters. I cannot find any way to do it so would be glad of some help.
You can create your own NumberFormat implementation, but easier than that you can do something like this:
String hexString = "0x20000";
int hexInt = Integer.parseInt(hexString.substring(2), 16);
String stringRepresentation = new String(Character.toChars(hexInt));
System.out.println(stringRepresentation); //prints "π €€"

Categories

Resources