I am trying to convert a large decimal value (the ID from a CAN DBC file) like 2566848554 into a hex string.
When I read the value into a long and use debug, B4J correctly displays the value as 0x18FF002A
but when I use byte converter as below, it returns the wrong value (0x98FF002A):
Debugging gets to be very confusing because the byte array is displayed as signed.
It may be because there is no unsigned variable of any type and I am getting very confused about the best way to deal with that...
If I replace Dim l(1) As Long with Dim l(1) As Int, and use bc.IntsToBytes(), I get the same result.
I fixed it as below, but this can't be the only way or the best way:
Any help appreciated
When I read the value into a long and use debug, B4J correctly displays the value as 0x18FF002A
but when I use byte converter as below, it returns the wrong value (0x98FF002A):
B4X:
Sub getIDinHex( in As Long ) As String
Dim bc As ByteConverter, out As String
Dim l(1) as Long
l(0) = in
Dim byteArray() As Byte = bc.LongsToBytes( l )
' Convert the byte array to a hexadecimal string
out = bc.HexFromBytes( byteArray )
out = "0x" & Regex.Replace( "^0+", out, "" )
Return out
End Sub ' getIDinHex()
Debugging gets to be very confusing because the byte array is displayed as signed.
It may be because there is no unsigned variable of any type and I am getting very confused about the best way to deal with that...
If I replace Dim l(1) As Long with Dim l(1) As Int, and use bc.IntsToBytes(), I get the same result.
I fixed it as below, but this can't be the only way or the best way:
B4X:
If byteArray(0) < 0 Then byteArray(0) = byteArray(0) + 128
Any help appreciated