B4J Question Convert HEX to Float

Philip Prins

Active Member
Licensed User
Longtime User
Hello

I get a Hex string "3719" that i want to convert to Decimal 14105 .
How can i do this?

Tnx in advance
Philip
 

wonder

Expert
Licensed User
Longtime User
Once you write it, yes!

EDIT: There probably is, buried in some library...

EDIT2: My bad, of course there is!!! Bit.ParseInt() with radix 16.

EDIT3: Do you specifically need a 32-bit float value?
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
I've actually wrote this C code two days ago... I can compile it

B4X:
#include <inttypes.h>

typedef unsigned char byte;

int64_t Int64FromHex(char* data)
{
    int64_t value;
    byte i, j, k;
    byte n = 7;
    for (i = 2; i < 17; i += 2)
    {
        j = i - 2;
        k = i - 1;
        byte a, b;
        if (data[j] >= '0' && data[j] <= '9')
        {
            a = data[j] - 48; // data[j] - '0'
        }
        else if (data[j] >= 'A' && data[j] <= 'F')
        {
            a = data[j] - 55; // data[j] - 'A' + 10
        }
        else if (data[j] >= 'a' && data[j] <= 'f')
        {
            a = data[j] - 87; // data[j] - 'a' + 10
        }
        else
        {
            a = 0;
        }
        if (data[k] >= '0' && data[k] <= '9')
        {
            b = data[k] - 48; // data[j] - '0'
        }
        else if (data[k] >= 'A' && data[k] <= 'F')
        {
            b = data[k] - 55; // data[j] - 'A' + 10
        }
        else if (data[k] >= 'a' && data[k] <= 'f')
        {
            b = data[k] - 87; // data[j] - 'a' + 10
        }
        else
        {
            b = 0;
        }
        ((char*)(&value))[n] = (a * 16) + b;
        --n;
    }
    return value;
}
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
I get the HEX from an arduino, it is a float converted to int
In C
B4X:
int16_t convertoFloatToInt16(float value, long max, long min) {
  float conversionFactor = (float) (INT16_t_MAX) / (float)(max - min);
  return (int16_t)(value * conversionFactor);
}

This is send and received as a Hex string ,max 4 characters like 5836.

I need to convert it back in a float like 21.37 .

I tried Byte converter BC.FloatsFromBytes(BC.HexToBytes(hex))(0) but the string is to short, should i add 0000?
 
Upvote 0

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
As @wonder said, the Bit.ParseInt("5836", 16) will give you an integer value for the hex string. You can then do whatever math you need to the integer value to convert it to your decimal value.

The byteconverter would be used if you needed each individual byte into any array (e.g. you needed "58" in one value and "36" in another value).
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
Not tested...

B4X:
#include <inttypes.h>

typedef unsigned char byte;

float FloatFromHex(char* data)
{
    float value;
    byte i, j, k;
    byte n = 3;
    for (i = 2; i < 9; i += 2)
    {
        j = i - 2;
        k = i - 1;
        byte a, b;
        if (data[j] >= '0' && data[j] <= '9')
        {
            a = data[j] - 48; // data[j] - '0'
        }
        else if (data[j] >= 'A' && data[j] <= 'F')
        {
            a = data[j] - 55; // data[j] - 'A' + 10
        }
        else if (data[j] >= 'a' && data[j] <= 'f')
        {
            a = data[j] - 87; // data[j] - 'a' + 10
        }
        else
        {
            a = 0;
        }
        if (data[k] >= '0' && data[k] <= '9')
        {
            b = data[k] - 48; // data[j] - '0'
        }
        else if (data[k] >= 'A' && data[k] <= 'F')
        {
            b = data[k] - 55; // data[j] - 'A' + 10
        }
        else if (data[k] >= 'a' && data[k] <= 'f')
        {
            b = data[k] - 87; // data[j] - 'a' + 10
        }
        else
        {
            b = 0;
        }
        ((char*)(&value))[n] = (a * 16) + b;
        --n;
    }
    return value;
}
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Tnx But this is in C,i need it for B4J

If i use Bit.ParseInt("5836", 16) i only get the decimal 22
I need 22582
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…