B4R Question Saving Double to Eeprom

petrbury

Member
Licensed User
Longtime User
Hi,
what's the easiest way to save Double number (for example -12.3456789) to Eeprom ?
Thank's
Petr
 

petrbury

Member
Licensed User
Longtime User
Thank You Erel, but apparently I'm missing something.
I tried :

B4X:
Sub Conv
    Dim  Koef As Double = 12.3456789
    Dim bc As ByteConverter
    Dim Tmp() As Byte

    Tmp = bc.DoublesToBytes(Array As Double(Koef))
   
    Log (Tmp(0))
    Log (Tmp(1))
    Log (Tmp(2))
    Log (Tmp(3))
    Log (Tmp(4))
    Log (Tmp(5))
    Log (Tmp(6))
    Log (Tmp(7))
    Log (Tmp(8))
End Sub

And the result is :

230
135
69
65
Out of bounds error. Array length = 4, Index = 4
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User

Check this example and imagine that a double value has 4 byte length.
Also take care about accuracy of Arduino Floats, they have only
6-7 decimal digits of precision.

B4X:
Sub convert_db_bd
    ' convert to Bytes
    Dim BC As ByteConverter
    Dim dbl As Double = 12.3456789
    Dim bytes() As Byte = BC.DoublesToBytes(Array As Double(dbl))
    Log("dbl=",dbl)
    Log("4 bytes: ", bytes(0),TAB,bytes(1),TAB,bytes(2),TAB,bytes(3))
   
    ' convert to Double
    Log("- - - - - - - -")
    Dim b(4) As Byte
    Dim BC As ByteConverter
    BC.ArrayCopy2(bytes, 0, b, 0, 4)
    Dim d() As Double = BC.DoublesFromBytes(b)
    Log("d():",d(0))  
End Sub
 
Last edited:
Upvote 0
Top