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?
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)
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
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
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.
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.