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

问题描述

我正在尝试创建一个函数,该函数将接受一个浮点变量并将其转换为一个字节数组.我找到了一段有效的代码,但如果可能的话,我想在函数中重用它.

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

我对指针等不太熟悉,但我读到 (float) 正在使用强制转换或其他什么?

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

任何帮助将不胜感激!

干杯

*已解决

这是我在 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 分钟里我学到了很多关于指针和引用的知识,Cheers Stack Overflow!

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

推荐答案

最简单的方法是建立联合:

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

输出:

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

或者真正破解它:

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

注意 - 在任何一种情况下,我都会确保将数据复制到作为输入参数给出的位置.这一点很重要,因为局部变量在你返回后将不存在(虽然你可以声明它们static,但我们不要教你坏习惯.如果再次调用函数怎么办......)

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函数将浮点数转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 02:45