I am new to the Arduino and having problem converting Big Endian 32 bit hex to float.
Example, Need to convert "429C0000" to "78"
Can anyone assist with this please?
I am new to the Arduino and having problem converting Big Endian 32 bit hex to float.
Example, Need to convert "429C0000" to "78"
Can anyone assist with this please?
First, you've got to convert the string "429C0000" to binary, then a simple 'float' cast is all you need.
If you've alread got it as binary:
unsigned long x = 0x429C0000;
Serial.println (*(float*) &x);
Her's one approach which solves the endianness and ascii/binary conversion:
void setup()
{
Serial.begin(57600);
float f = strtof("429C0000");
Serial.println(f);
}
float strtof(char *s)
{
unsigned long ul = strtoul(s,0,16);
float f = *(float *)&ul;
return f;
}
void loop()
{
}
You guys make this look so easy.
It has been a while since i did any C.
If you don't mind, helping once again - might save me a few days LOL
I am receiving data via serial such as "22 20 20 70 20 05 10 01 22 20 20 70 20 20 10 02 42 9C 00 00 10 03 87 C9"
Is there an easy way to parse the "429C0000" from the result to a variable to your function?
I have been doing something wrong here as it is not working.
You have to know where about in the stream your number is. Is it always preceded by 02 or can it be anywhere. Data streams like this normally have a header which tells you when they start. Do you know the format of your data?
Thanks for the assistance.
I have been trying to figure out how parse the stream to grab the data i need.
Header always starts with the - "04"
It's length is either 46 or 50 chars and chop out what i need.
Here is a received stream.
0422202070200510012220207020201002429C000010037683
042220207020051001222020702020 1002[429C0000]1003 7683
[ The Data]
For the CRC calc, I need this much of the stream to pass to a CRC16 Function. Seems easy, but every Approach I try on the Arduino, results in some error LOL
10012220207020201002429C00001003
The last word is CRC 16 of all the Data from 1001 to 1003
BTW - Do you know if the led#13 rapid flashing is a dead processor or board failure?
The problem is that you are not telling us enough information, you are just supplying bits that are hard to put together.
Where is the data coming from?
How does it arrive? ASCI, or raw hex or what?
It's length is either 46 or 50 chars and chop out what i need.
So is there a way of telling which it is?
Does the data you want always start a fixed number of characters in from 04
Grumpy_Mike,
Sorry for the confusion, I haven't done any C in a very long time.
I am communicating with a piece of equipment.
I send a Command to query the equipments temperature.
All of the Data that is received (Serially) is in HEX exactly as follow: 0422202070200510012220207020201002429C000010037683
Valid response starts with 04
22 = DeviceId
20 = Address
20 = Command1 (second instance of 20)
70 = Command2
20 = ACK
05 = End of Transmission Data that follows is requested result data
Hex values between "1002 & 1003" is result temperature.
Trying to find best way to process the data on receive 2 bytes as hex values versus chars 1 at a time or is it simpler to capture all to a string, then pick it apart?
My thoughts were to:
Receive the serial string into recArray,
char recArray[50];
devId == strncpy(recArray, 2,3);
addr == strncpy(recArray,4,5);
Not sure how to search for the instance of "1002"
Any thought would be helpful..
Thanks.
I would handle it serially as it comes in.
Look for the 04 to arrive, when it does go into a routine that pulls the bytes out two at a time.
Hex values between "1002 & 1003" is result temperature.
I don't understand this. Does it mean that anything between the markers 1002 & 1003 is a temperature or is it the values?
If you always pull bytes out two at a time say called B1 and B2 then you can combine them by using shift and logic OR and do a compare:-
val = b2 | (b1 << 8); // assume b1 is most significant byte
if(val == 0x1002) { // found start of temperature stuff
Thanks Mike,
I had not considered the Logic Or & Shift approach.
Once I find the "1002" marker, I can loop for the next 8 bytes for the Big Endian value to convert to float.
I see there is a Serial.Peek() function, wasn't sure if there was a better way to parse but your approach might be the easier way to go.
I appreciate the help.