# Convert Int to 32-Bit Float Hex

Need help converting a decimal number to a 32-Bit Hex Representation. Example // 300(DEC) to 43960000 (HEX) and stuff into a 4 byte[4] array.

``````unsigned long val = 300;
float fval = (float)val;
byte* array = (byte*) &fval;
``````

It's pretty much the same solution as you asked for here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1285483881/14#14

Thanks for the reply, but the code as provided just generates errors using version .21.

I guess i'm asking again, as previous examples as you so noted didn't help me either.

Works here on 0017.
I don’t have 0021, so I’ll just have to imagine what your errors might be.

Edit: and on 0018:

``````unsigned long val = 300;
float fval = (float)val;
byte* array = (byte*) &fval;

void setup ()
{
Serial.begin (9600);
for (int i = 0; i < 4; ++i) {
Serial.println (array [i], HEX);
}
}

void loop ()
{}
``````

Compiled and verified.

Thank you. As i had mention early on, I do not know C all that well.

I had copy & pasted code in a case loop. Not sure as to why certain things have to be outside Loop() to work.

I assume the processor is "Little Endian" as the way the values are stuffed into the array. val of 350 returns 0x80,0x83,0x43.

In order to send "Big Endian" i can do sizeof(array) and stuff a new array with values in reverse order and pad with remaining 0x00 unless you have a more correct way to do that with the float function.

In order to send "Big Endian" i can do sizeof(array) and stuff a new array with values in reverse order and pad with remaining 0x00 unless you have a more correct way to do that with the float function.

There is no need to create another array. Simply Serial.print the existing array in the reverse order.

Paul, Yes I could do that if i was only going to display the results. After converting to Big Endian, it gets added to other data to have CRC16 done, then all the data is pumped out serially.

I have it working by swapping the order. In trying to learn C, I like to TRY to do things the most efficient way possible, instead of my loose coding I do now. LOL

I hate to ask dumb questions, but the best knowledge comes from the experts.

In trying to learn C, I like to TRY to do things the most efficient way possible, instead of my loose coding I do now. LOL

You should try to learn by doing it in a way you understand not try for efficiency. If you don't understand something you do in your program, work on it until you understand it. Completely. If there's something you don't go back over the books and learn some more. If you wonder what's the difference between and array, a pointer, a reference and a laser gun, go back and work trough primers about C and space battles until you do understand that. Repeat until you understand the subject, then move on to the next mystery.

I hate to ask dumb questions, but the best knowledge comes from the experts.

No, the best knowledge comes from what you put some work into understanding yourself. Experts at best will point you in the right direction, but at the level you have problems, you don't need experts. You need a book like "Learning C in 12 easy steps" and work through that.

Not sure as to why certain things have to be outside Loop() to work.

Things inside a function belong to that function, things outside don't. So the general rule is, put stuff inside a function if it belongs to it, outside if you need to share it.

Korman