C功能转换浮到字节数组 [英] C Function to Convert float to byte array

查看:266
本文介绍了C功能转换浮到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让将接受一个浮点型变量,并将其转换为字节数组的功能。我发现code的作品的片断,而是想如果可能的话再使用它的功能。

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.

我还与Arduino的环境中工作,但我明白,它接受大多数C语言。

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

目前的工作:

float_variable = 1.11;
byte bytes_array[4];

*((float *)bytes_array) = float_variable;

什么我可以改变这里,使这个函数的工作?

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;  
}

我不那么熟悉的指针等,但我读到的(浮动的)是用铸造的东西?

任何帮助将大大AP preciated!

Any help would be greatly appreciated!

干杯

*编辑:解决

下面是我在Arduino的人谁发现这个工程的最终功能。有下面的答案更有效的解决方案,但是我觉得这是好理解的。

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.

功能:将输入浮点变量的字节数组

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);
}

调用函数

float float_example = 1.11;
byte bytes[4];

float2Bytes(float_example,&bytes[0]);

感谢大家的帮助下,我学到了很多关于指针和过去20分钟参考,干杯堆栈溢出!

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

推荐答案

最简单的就是让工会:

#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;
}

输出:

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…)

这篇关于C功能转换浮到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆