B4J Question Byte array to double

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I have a B4R app that reads data from an RS232 device. I am sending the contents of the communications buffer in bytes to a B4J app using 'astream.write(buffer) in B4R.

in B4R I use this code to get the data to display a reading (data is a double)

B4X:
Sub astream_newData (Buffer() as byte)
    Dim b(4) as byte
    Dim bc as ByteConverter
    bc.ArrayCopy2(Buffer, 2, b, 0, 4)
    Dim d() as double = bc.DoubleFromBytes(b)
    log("Reading = ",d(0))
End Sub

This works in B4R but not in B4J.

B4X:
    Dim bc as ByteConverter
    Dim s(4) As Byte
    bc.ArrayCopy(Buffer,6,s,0,4)
    Dim d() As Double = bc.DoublesFromBytes(s)
    Log("d = "&d(0))

In B4J I get the following error - note: when the array is sent from B4R to B4J 4 extra bytes is added. All data after "0F000000" is correct so I use an offset of 6 instead of 2 in the array copy.

a = 0F000000AA103A86A73C0000000048006000FB len = 19
Error occurred on line: 72 (Main)
java.lang.ArrayIndexOutOfBoundsException: 0
at b4j.example.main._astream_newdata(main.java:142)

Any one have any ideas?

Thanks
 

atiaust

Active Member
Licensed User
Longtime User
Thanks Erel,

I tried using B4RSerializator with out much success.

Can you point me in the right direction please.

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thank Erel,

If I use B4RSerializator to convert the buffer how do I write it out in B4R?

B4X:
    Dim ser as B4RSerializator
    Dim be(15) As Object 'used as a storage buffer.
    Dim objects() As Object = ser.ConvertBytesToArray(Buffer, be)
    astream.Write = ????
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,
I am able to get the value in B4J using the Float as you suggested earlier.

I now have a float number, example '0.023456789' using byteconverter bc.FloatsFromBytes(s)

How can I get it as a string value with 3 decimal places. formatnumber doesn't seem to work for a float?

Thanks
 
Upvote 0
Top