B4J Question SOLVED - ByteConveter not returning array of ints?

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I was posting the below code when I saw something that fixed the problem but am still posting this in case someone else runs into this problem

The below code was not working because my hex string only created 2 bytes and I guess byteconverter needs 4 bytes to make an int array
THIS seems like a byteconverter bug to me - it should see there aren't 4 bytes but still return and array of ints.

I am processing Pronto Hex strings and they are in groups of 4 bytes. To make the code below work.
What I did was add pHexCode = "0000" &pHexCode this make the HexToBytes produce 4 bytes that then made IntsFromBytes work


I have the following code (I am sure I am doing something dumb but just can't see it)

B4X:
Public  Sub ConvertCode(pHexCode As String) As Int                             ' <---------------------------- this value is "007e"
        
           if  pHexCode.Length = 4 then                                                     '<------------------- Check length if 4 make it 8 otherwise not enough bytes to make ints
               pHexCode = "0000" &pHexCode                                            '<------------------- THIS line will make the code below work
           end if                                                                                        '<-------------------

            Dim bc                 As ByteConverter
             
             Dim HexBytes()     As Byte = bc.HexToBytes(pHexCode)             '<-------------------- Gets converted to 2 byes 0 and 7E
            Dim HexResult()      As Int  = bc.IntsFromBytes(HexBytes)            '<-------------------- DOES not return anything

          return HexResult(0)                                                                    '<-------------------- FAILS because HexResult does not contain anything
end sub
 

William Lancee

Well-Known Member
Licensed User
Longtime User
You actually don't need the IntsFromBytes (which expects 4 bytes per int).

B4X:
Public Sub ConvertCode(pHexCode As String) As Int                             ' <---------------------------- this value is "007e"
    Dim bc As ByteConverter
    Dim HexBytes() As Byte = bc.HexToBytes( pHexCode)             '<-------------------- Gets converted to 2 byes 0 and 7E
    Return 256 * HexBytes(0) + HexBytes(1)
End Sub
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
It occurred to me in the middle of the night that my post was in error. The integer cast of byte runs from -127 to 127.
If you have a number greater than 127 in the byte, you get the wrong result. Here is the corrected routine, less compact.

B4X:
Public Sub ConvertCode(pHexCode As String) As Int                             ' <---------------------------- this value is "007e"
    Dim bc As ByteConverter
    Dim HexBytes() As Byte = bc.HexToBytes( pHexCode)             '<-------------------- Gets converted to 2 byes 0 and 7E
    Dim int0 As Int = HexBytes(0)
    If int0<0 Then int0 = 256 + HexBytes(0)
    Dim int1 As Int = HexBytes(1)
    If int1<1 Then int1 = 256 + HexBytes(1)
    Return 256 * int0 + int1
End Sub
 
Upvote 0
Top