Android Question Odd Behaviour converting HEX to Float to String

Budley

New Member
Licensed User
Longtime User
Hello Forum,
I'm trying to convert a string of Hex, from a serial input, to it's decimal Float value then to String for a Textbox and Listview.
I get it to Float - the debug window displays the correct value, but it seems that it's not really a number because I can't do anything with it after that.

Here's the code:
B4X:
Dim sRx As String
Dim fRx As Float
Dim dRx As Double

    sRx = "43030FF4"    'Hex string = 131.06232
    fRx = Conv.FloatsFromBytes(Conv.HexToBytes(sRx))'correctly converts String to Float
    Dim dRx As Double = fRx    'Float is correct, but this assignment don't work
    fRx = fRx * 10            'This don't work - NumberFormatException
    txtResult.Text = fRx    'This don't work.
    sRx = NumberFormat(fRx,0,2)    'This don't work - NumberFormatException
    ListView1.AddSingleLine("PVar: " & sRx)    'This is what it's all for

I've tried each of these lines one at a time and in combinations. Always I get a NumberFormatException error. I can't avoid the Hex String input, I have to deal with it. I'm open to suggestions on how to get it into a format that will display on the screen as a real (decimal) number.
I'm testing on an Android 2.2.2 device. I dunno if that's too old.

Any thoughts on what I'm doing wrong?

Ta, Michael.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
I've not used the byteconverter class before but looking at the documentation it appears that you are getting an array of floats back from your conversion which is probably why it's not converting to a double.
15.png
FloatsFromBytes (bytes() As Byte) As Float()

Takes an array of Bytes and returns an array of Floats converted from that array.

I'd test this for you but only got my tablet to hand at the moment.

Regards,
RandomCoder
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
Just change the line to
B4X:
fRx = Conv.FloatsFromBytes(Conv.HexToBytes(sRx))(0)
The (0) at the end will grab the first element of the returned array.
 
Upvote 0

Budley

New Member
Licensed User
Longtime User
Just change the line to
B4X:
fRx = Conv.FloatsFromBytes(Conv.HexToBytes(sRx))(0)
The (0) at the end will grab the first element of the returned array.
Thanks James,
That got it. Luv your work.
I must've read the Byteconverter tutorial a dozen times, and I'm sure I still don't understand it.

Cheers,
Michael.
 
Upvote 0
Top