In my Java Program, I need a byte array from a Hex String. So I do the following:
byte[] key=HexUtils.convert("0AA387ED291F6D90");
This converts the Hex String into a byte array as follows:
Output: Byte Array: key:[10, -93, -121, -19, 41, 31, 109, -112]
Now, I want to move the Hex String into the properties file. But I am not able to do so.
Try 1:
Properties file ====> key=0AA387ED291F6D90
Java Program ====> byte[] key = HexUtils.convert(prop.getProperty("key"));
This gives error: BAD HEXADECIMAL DIGIT
Try 2:
Properties file ====> key=\u000A\u00A3\u0087\u00ED\u0029\u001F\u006D\u0090
Java Program ====> byte[] key = HexUtils.convert(prop.getProperty("key"));
Output: key:[10, -93, 63, -19, 41, 31, 109, 63]
If you notice, 2 bytes are different than the Expected. Hex 87 and Hex 90. Both are converted to 63. Whereas I am expecting -121 and -112 respectively.
Can somebody please help me as to how do I do this conversion. I hope I am clear!
Angad
If using the
\uxxxxcodes, you’re using UNICODE escape sequences.\u0090is not going to end up being interpreted as String"90", it’s gonna end up as a single character that corresponds to code unit 0090 in the UTF-16 encoding.Your first approach should work. I suggest you try
System.out.println(prop.getProperty("key"));or some other form of output and check if there’s leading or trailing white space, some character you didn’t expect ornull. The latter would mean the property for “key” isn’t found.