Convert a string of bits to unicode character in java - 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

Related

Convert integer, signed integer to EBCDIC in java

How to convert an unsigned integer to EBCDIC format while sending to mainframe, suppose if want to encode 4550 to ebcdic format, below snippet i’m trying, As per the ebcdic chart numbers doesn’t have the equivalent symbol to be encoded and i’m always getting the blank result
String s = “4550”;
String e = new String(s.getBytes(),“Cp037");
Can someone please help me with the steps to encode it to EBCDIC
Mainframe expecting to be it in encoding format, when they consume the request, numbers field should be unreadable format, here is the example
C ¤,G ÚM P1234 N
fields which are in alphanumeric it’s in readable format and few fields which are in numeric it is encoded with symbols, i’m looking for a way to achieve the same
I found some solutions online which converts integer to packeddecimal to ebcdic format, but it didn't work as expected.
You need to be very clear on what is required on the Mainframe end is it pure text or is it a binary format (e.g. Cobol Comp, comp-3).
Do you need to convert it to EBCDIC. If it is just Text. Just create it as normal Text and let the Mainframe Transfer software do the translation to EBCDIC.
Java strings are 16 bit (i.e. small int) unicode. EBCDIC is a 8 bit char encoding, it is best represented as a Byte array or Stream. To convert a java String to EBCDIC:
byte[] ebcdicData = "0123".getBytes("cp037");
I found the solution of way of encoding, below is the snippet
int val = Integer.parseInt(input);
if (val >= 0 && val <= 0xffff) {
hex = String.format("%04x", val);
} else {
hex = String.format("%08x", val);
}
String[] split = hex.split("(?<=\\G.{" + 2 + "})");
String result = "";
for (String sp : split) {
byte b1[] = {(byte) Integer.parseInt(sp, 16)};
result += new String(b1, "Cp037");
}

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 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.

Prepend a byte to a byte array

I need to prepend the string "00" or byte 0x00 to the beginning of a byte array? I tried to do it with a for loop but when I convert it to hex it doesn't show up in the front.
The string "00" is different than the number 0x00 when converted to Bytes. What is the data type you are trying to prepend to your byte array? Assuming it's a Byte representation of the string "00", try the following:
bytes[] orig = <your byte array>;
String prepend = "00";
bytes[] prependBytes = prepend.getBytes();
bytes[] output = new Bytes[prependBytes.length + orig.length];
for(i=0;i<prependBytes.length;i++){
output[i] = prependBytes[i];
}
for(i=prependBytes.length;i<(orig.length+prepend.lenth);i++){
output[i] = orig[i];
}
or you can use Arrays.copy(...) instead of the two for loops as mentioned before to do the prepending. See How to combine two byte arrays
Alternativly, if you are trying to literally prepend 0 to your byte array, decalare prependBytes in the following way and use the same algorithm
byte[] prependBytes = new byte[]{0,0};
Also you say that you're converting your byte array to hex, and that may truncate leading zeros. To test this, try prepending the follwoing and converting to hex and see if there is a different output:
byte[] prependBytes = new byte[]{1,1};
If it is removing the leading zeros that you want, you may wish to convert your hex number to a string and format it.

Categories

Resources