簡體   English   中英

Arduino 將浮點數轉換為十六進制 IEEE754 單精度 32 位

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

我想在 Arduino 的以下站點中將浮點值轉換為 IEEE754 單精度 32 位十六進制值。 https://www.binaryconvert.com/result_float.html?decimal=051046049048

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

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

如何創建這樣的 function? 如果格式不同也沒關系。

f已經以二進制形式存儲。 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