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.
Related
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");
}
I have some string data like
� ;� ;
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: 😊 ; - 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
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
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
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.