I have code that generates a random byte array (4 bytes) that I am encoding Hex and then using the HexUtils hexTringToInt to convert to a number. I get frequent failures.
Example responses:
- [byteArray:[82, -102, 89, 6], hexValue:529a5906, integerValue:1385847046]
- [byteArray:[-55, -51, 114, -100], hexValue:c9cd729c, ERROR:java.lang.NumberFormatException: For input string: "c9cd729c"]
- [byteArray:[95, 13, -16, -13], hexValue:5f0df0f3, integerValue:1594749171]
- [byteArray:[51, 69, -53, 60], hexValue:3345cb3c, integerValue:860212028]
- [byteArray:[-81, -10, -75, -117], hexValue:aff6b58b, ERROR:java.lang.NumberFormatException: For input string: "aff6b58b"]
Code:
byteArray is the byte arrray received as part of a byte response from an external Server.
String hexSeq = byteSeq.encodeHex()
Integer seqNo = state.seqNo
Map errorResp = [byteArray: byteSeq, hexValue: hexSeq]
try {
seqNo = hubitat.helper.HexUtils.hexStringToInt(hexSeq)
errorResp << [integerValue: seqNo]
} catch (error) {
errorResp << [ERROR: error]
}