简体   繁体   English

Arduino 将浮点数转换为十六进制 IEEE754 单精度 32 位

[英]Arduino convert float to hex IEEE754 Single precision 32-bit

I would like to convert float values to IEEE754 Single precision 32-bit Hex values in the following site on Arduino.我想在 Arduino 的以下站点中将浮点值转换为 IEEE754 单精度 32 位十六进制值。 https://www.binaryconvert.com/result_float.html?decimal=051046049048 https://www.binaryconvert.com/result_float.html?decimal=051046049048

float f = 3.10;
byte hex[4] = {0};

byte FloatToHex(float f){
   .......
}

How can I create a function like this?如何创建这样的 function? It's okay if the format is different.如果格式不同也没关系。

f is already stored in binary. f已经以二进制形式存储。 reinterpret_cast is generally a code smell issue, but its valid use is to view the byte representation of variables. reinterpret_cast通常是代码异味问题,但它的有效用途是查看变量的字节表示。


void FloatToHex(float f, byte* hex){
  byte* f_byte = reinterpret_cast<byte*>(&f);
  memcpy(hex, f_byte, 4);
}

void setup() {
  float f = 3.10;
  byte hex[4] = {0};

  FloatToHex(f, hex);
  //... do stuff with hex now...
}

void loop() {
  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM