Android Question How to convert 4 byte array to 32bit integer?

evansus

Member
Licensed User
Longtime User
I posted a question in B4R forum since there were some close answers there but my issue is in B4A so am also asking in this forum.

I have an array of 4 bytes and want to convert these to a 32 bit integer
I have tried to use Byteconverter and also tried the method below. However, I don't think this is giving the right values since I need to read LittleEndian:
I tried reversing the order of the bytes (x=4 to 1) but this did not work either.
Any suggestions are well appreciated

B4X:
Dim b() as Byte
For x=1 To 4 
  u32 = u32 * 256 + Bit.AND(b(x), 255)
  Log("x=" & x & ";U32=" & u32)
 
 
  Next

Also I am confused with ByteConverter - If I convert an array of bytes to an array of ints can I then get the ints to one 32 bit integer? How do I do this?

Thanks
Evansus
 

udg

Expert
Licensed User
Longtime User
Hi,

are you aware that in B4R
Byte: 0 - 255
Int (2 bytes): -32768 - 32768. Similar to Short type in other B4X tools.
UInt (2 bytes): 0 - 65535
while in B4A
Byte: -128 --> +127 ?

Edit (java/B4A):
Short : 2bytes signed: -32768 to 32767
Int : 4bytes signed: -2147483648 to 2147483647
Long : 8bytes signed: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (9.2...E+18)

udg
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
the shortest in typing must be this

B4X:
Dim bytes() As Byte=Array As Byte(0x00,0x00,0xff,0xff)
Dim v As Int
For x=0 To 3
    v=v*256+(256+bytes(x)) mod 256
Next
Log(v)

or

B4X:
Dim bytes() As Byte=Array As Byte(0x00,0x00,0xff,0xff)
Dim v As Int
For x=0 To 3
    v=v+((256+bytes(x)) Mod 256)*Power(256,3-x)
Next
Log(v)

or the cleaner method

B4X:
Dim bytes() As Byte=Array As Byte(0x00,0x00,0xff,0xff)
Dim v As Int
For x=0 To 3
    v=Bit.ShiftLeft(v,8)+Bit.And(256+bytes(x),255)
Next
Log(v)
 
Last edited:
Upvote 0

evansus

Member
Licensed User
Longtime User
B4X:
Dim bc As ByteConverter
bc.LittleEndian = True
Dim i As Int = bc.IntsFromBytes(Array As Byte(1, 2, 3, 4))(0)
Log(i)

Thanks Sorex and Erel - your help is much appreciated and I think these answers will help many others like me with Byte math issues
I will try the byteconverter approach so I can change littleEndian
 
Upvote 0

evansus

Member
Licensed User
Longtime User
B4X:
Dim bc As ByteConverter
bc.LittleEndian = True
Dim i As Int = bc.IntsFromBytes(Array As Byte(1, 2, 3, 4))(0)
Log(i)

Hi Erel
I can only use ByteConverter for 3 bytes or more
I get errors when I try to do the same with 2 bytes
I tied to make the following code

B4X:
Sub convert2BytesToINT(b1 As Byte,b2 As Byte,blittleEndian As Boolean) As Int
'use byteconverter
Dim retint As Int=0 'return value
Dim bc As ByteConverter
bc.LittleEndian=blittleEndian
'Dim i As Int = bc.IntsFromBytes(Array As Byte(1, 2, 3, 4))(0) 'Erels examplecode
Try
    retint=bc.IntsFromBytes(Array As Byte(b1,b2))(0)
    Log("retint=" & retint)
Catch
    Log("Err in byteconverter" & CRLF & LastException.Message)
   
End Try

Return retint

The Catch returns
java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0


I only wrote this procedure because I was getting errors with
B4X:
Try
    lastgood=0
    Dim i As Int = bc.IntsFromBytes(Array As Byte(bx1,bx2,bx3))(0) 'This works OK
Log("i=" & i)
Catch
    Msgbox("Err at " & lastgood & CRLF & LastException.message,"Err in Byte converter")
    Log("lastGood=" & lastgood & CRLF & LastException.message)
End Try
Try
    lastgood=1
    Dim i As Int = bc.IntsFromBytes(Array As Byte(bx1,bx2))(0) 'This gave an error
    Log("i(1,2)=" & i)
Catch
    Msgbox("Err at " & lastgood & CRLF & LastException.message,"Err in Byte converter")
    Log("lastGood=" & lastgood & CRLF & LastException.message)
End Try

Got error in call below lastgood=1

Any suggestions on how to handle this?
Thanks
 
Upvote 0

evansus

Member
Licensed User
Longtime User
The first step is to remove the try / catch block.

Int = 32bit = 4 bytes.
Short = 16bit = 2 bytes.
If you want to convert 2 bytes then use bc.ShortsFromBytes.
If you want to convert 3 bytes then add a zero as the first byte and convert it with IntsFromBytes.
Thanks Erel
I will try both your suggestions
 
Upvote 0
Top