B4R Question buffer (byte)value to double

atiaust

Active Member
Licensed User
Longtime User
Hi All,

I am trying to read a double or float value from bytes 3 to 6 in a 15 byte array.

The string from bc.HexFromBytes = "AA101DFCA43C0000000048006000A5"

B4X:
Sub astream2_newData (Buffer() As Byte)
'
Dim a As String = bc.HexFromBytes(Buffer)   'contents of buffer in Hex       
    Log("buffer len = ",Buffer.Length)                'len of buffer = 15
    Log (bc.SubString2(a,4,12))                        'get bytes 3 thru 6 from string    =     1DFCA43C

How do I convert to a number and format in text?

Thanks
 

atiaust

Active Member
Licensed User
Longtime User
Thanks Erel,

I could not get that line of code to work as you wrote it. I changed to below and the program crashes.
Any ideas

B4X:
     Dim a As String = bc.HexFromBytes(Buffer)        'contents of buffer in Hex        =    AA101DFCA43C0000000048006000A5
     Dim d() As Double = bc.DoublesFromBytes(bc.SubString2(a,3,7))
'
    Log ("d len = ",d.Length)        'length = 1
    Log ("d = ",NumberFormat(d(0),1,2))        'crashes program



AppStart
rRS232 Ver 1.0
buffer len = 15
E541773C
d len = 1
Exception (9):
epc1=0x40202def epc2=0x00000000 epc3=0x00000000 excvaddr=0x3fff0907 depc=0x00000000
ctx: cont
sp: 3ffefa30 end: 3ffefc90 offset: 01a0
>>>stack>>>
3ffefbd0: 3fff08fc 00000017 3fff0944 40202de4
3ffefbe0: 3fff092c 00000064 40203ae0 3ffeec70
3ffefbf0: 402010b2 00000005 00000005 0000000f
3ffefc00: 00000064 3fff08e4 3ffee8ec 4020222a
3ffefc10: 40203278 0000000d 3ffee918 40203160
3ffefc20: 00000000 0000000d 3ffee918 40203160
3ffefc30: 00002580 3ffee90c 3ffee918 3ffeec64
3ffefc40: 3fffdad0 3fff08d4 3ffee7b8 40202b00
3ffefc50: 3fffdad0 3ffee7c0 00000000 40202b6b
3ffefc60: 3fffdad0 00000000 3ffeec5c 40202f8b
3ffefc70: feefeffe feefeffe feefeffe 40203b2c
3ffefc80: feefeffe feefeffe 3ffeec70 40100710
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(3,6)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Erel,

Further my original post.

The 4 byte string "1DFCA43C" is a 'float - Little Endian (DCBA)' IEEE745 number.

The Hex string is actually 0.0201397482 when converted using an online Hex Converter.

Can I convert this using B4R?

Thanks
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Hi Erel,

The line of code in post #5 above crashes the WEMOS device - error as above in post #3.

In post #4 above I am asking how to convert a 4 byte little Endian hex value to decimal.

Is this possible in B4R?

Thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This was a tricky one.

Correct code:
B4X:
Dim bc As ByteConverter
Dim Buffer() As Byte = bc.HexToBytes("AA101DFCA43C0000000048006000A5") 'you don't need this line
Dim b(4) As Byte
bc.ArrayCopy2(Buffer, 2, b, 0, 4)
Dim d() As Double = bc.DoublesFromBytes(b)
Log(d(0))
The reason that the previous code failed is related to the memory alignment requirement of ESP8266 / ESP32.
Instead of creating a sub-array with bc.Substring, we are copying the data to a new array.
The subarray would have shared the same memory with the original array however it doesn't start at the beginning. This later caused DoublesFromBytes to fail because the double value must be memory aligned.
 
Upvote 0

atiaust

Active Member
Licensed User
Longtime User
Thanks Erel,

We all need challenges from time to time...

I have tested and it works well. Thank you.
 
Upvote 0
Top