Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Function to Convert float to byte array

Tags:

I'm trying to make a function that will accept a float variable and convert it into a byte array. I found a snippet of code that works, but would like to reuse it in a function if possible.

I'm also working with the Arduino environment, but I understand that it accepts most C language.

Currently works:

float_variable = 1.11; byte bytes_array[4];  *((float *)bytes_array) = float_variable; 

What can I change here to make this function work?

float float_test = 1.11; byte bytes[4];  // Calling the function float2Bytes(&bytes,float_test);  // Function void float2Bytes(byte* bytes_temp[4],float float_variable){       *(float*)bytes_temp = float_variable;   } 

I'm not so familiar with pointers and such, but I read that (float) is using casting or something?

Any help would be greatly appreciated!

Cheers

*EDIT: SOLVED

Here's my final function that works in Arduino for anyone who finds this. There are more efficient solutions in the answers below, however I think this is okay to understand.

Function: converts input float variable to byte array

void float2Bytes(float val,byte* bytes_array){   // Create union of shared memory space   union {     float float_variable;     byte temp_array[4];   } u;   // Overite bytes of union with float variable   u.float_variable = val;   // Assign bytes to input array   memcpy(bytes_array, u.temp_array, 4); } 

Calling the function

float float_example = 1.11; byte bytes[4];  float2Bytes(float_example,&bytes[0]); 

Thanks for everyone's help, I've learnt so much about pointers and referencing in the past 20 minutes, Cheers Stack Overflow!

like image 704
Ben Winding Avatar asked Jun 25 '14 23:06

Ben Winding


People also ask

Can we convert float to byte?

Float to Byte Array Conversion As we know, the size of a float in Java is 32 bit which is similar to an int. So we can use floatToIntBits or floatToRawIntBits functions available in the Float class of Java. And then shift the bits to return a byte array. Click here to learn more about bit shifting operations.

Can we convert float to char in C?

gcvt() | Convert float value to string in C This function is used to convert a floating point number to string. Syntax : gcvt (float value, int ndigits, char * buf); float value : It is the float or double value.

How do you convert int to Bytearray?

The Ints class also has a toByteArray() method that can be used to convert an int value to a byte array: byte[] bytes = Ints. toByteArray(value);

Can we convert float to byte in Java?

byteValue() is a built-in method in Java that returns the value of this Float as a byte(by casting to a byte). Basically it used for narrowing primitive conversion of Float type to a byte value.


Video Answer


1 Answers

Easiest is to make a union:

#include <stdio.h>  int main(void) {   int ii;   union {     float a;     unsigned char bytes[4];   } thing;    thing.a = 1.234;   for (ii=0; ii<4; ii++)      printf ("byte %d is %02x\n", ii, thing.bytes[ii]);   return 0; } 

Output:

byte 0 is b6 byte 1 is f3 byte 2 is 9d byte 3 is 3f 

Note - there is no guarantee about the byte order… it depends on your machine architecture.

To get your function to work, do this:

void float2Bytes(byte bytes_temp[4],float float_variable){    union {     float a;     unsigned char bytes[4];   } thing;   thing.a = float_variable;   memcpy(bytes_temp, thing.bytes, 4); } 

Or to really hack it:

void float2Bytes(byte bytes_temp[4],float float_variable){    memcpy(bytes_temp, (unsigned char*) (&float_variable), 4); } 

Note - in either case I make sure to copy the data to the location given as the input parameter. This is crucial, as local variables will not exist after you return (although you could declare them static, but let's not teach you bad habits. What if the function gets called again…)

like image 61
Floris Avatar answered Sep 20 '22 18:09

Floris