Hello
I get a Hex string "3719" that i want to convert to Decimal 14105 .
How can i do this?
Tnx in advance
Philip
I get a Hex string "3719" that i want to convert to Decimal 14105 .
How can i do this?
Tnx in advance
Philip
#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;
}
I get the HEX from an arduino, it is a float converted to int@Jeffrey Cameron, awesome!![]()
int16_t convertoFloatToInt16(float value, long max, long min) {
float conversionFactor = (float) (INT16_t_MAX) / (float)(max - min);
return (int16_t)(value * conversionFactor);
}
#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;
}
Tnx But this is in C,i need it for B4JNot 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; }
Solved it , 36 are the first 2 digits, 58 the second 2 digitsTnx 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