B4J Question Read bytes into Ints and strings

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hello All,
I am implementing a communications routine with an ATM pinpad.
I receive a string of bytes thru the socket newdata event, but I need to convert the first 4 bytes to int + the next 4 bytes to int and the remaining ones to charachters to read the receipt in plain text

The first 4 bytes contain the ResultCode of the transaction
the next 4 bytes contain the lenght of the following data
the remaining bytes are to be converted to text and contain the receipt data

Any Suggestions ?
 
Last edited:

Pedro Caldeira

Active Member
Licensed User
Longtime User
Hi Erel,
I just did that, but when i read the int at position 1 i should get 10, equivalente to 0A in hex, but I get 0.
but the hexfrombytes functions reads the correct value in Hex. Any idea Why ?

B4X:
Dim Raf As RandomAccessFile
Raf.Initialize3(Buffer,True)
Dim ResCode As Int=Raf.ReadInt(1)
Log(ResCode)
 
Private IngenicoReply As String=BtCvt.HexFromBytes(Buffer)
Log(IngenicoReply)

Data from the log

0 <-Raf

0A000000000000000024413038350A204C4F43414C20504F5341303737104F50455241C7C34F20414E554C41444100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Shouldn't it be:
Dim ResCode As Int=Raf.ReadInt(0) ?
 
Upvote 0

Pedro Caldeira

Active Member
Licensed User
Longtime User
It All works now woth the exception of String,as I said earler I need to read:

The first 4 bytes contain the ResultCode of the transaction (Int)
the next 4 bytes contain the lenght of the following data (Int)
the remaining bytes are to be converted to text and contain the receipt data (String)

I am using the following code But I keep getting a out of bounds error due to the size of the text

B4X:
    Dim Raf As RandomAccessFile
    Raf.Initialize3(Buffer,True)
    Dim OperationResult As Int=Raf.ReadInt(0)' Reads the first 4 bytes
    Dim TextLen As Int=Raf.ReadInt(4) 'read the second 4 bytes
    Dim Receipt As String=Raf.ReadObject(8) ' should read all text from postion 8 until the end of the array, right ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. ReadObject has nothing to do with strings. Tip: there is never a good reason to use raf.ReadObject.

2. You should use RAF.CurrentPosition.

3.
B4X:
Dim Raf As RandomAccessFile
    Raf.Initialize3(Buffer,True)
    Dim OperationResult As Int=Raf.ReadInt(Raf.CurrentPosition)' Reads the first 4 bytes
    Dim TextLen As Int=Raf.ReadInt(Raf.CurrentPosition) 'read the second 4 bytes
    Dim b(Raf.Size - Raf.CurrentPosition) As Byte
    raf.ReadBytes(b, 0, b.Length, Raf.CurrentPosition)
    Log(BytesToString(b, 0, b.Length, "ASCII"))
 
Upvote 0
Top