浮点数转byte数组

long a=123451; float b=34.56745f; float c=0.0; byte fbs[]={0,0,0,0}; byte* t=fbs; float2Bytes(t,b); unsigned int addrF=(unsigned int) &b; unsigned int addrFc=(unsigned int) &c; Serial.println(addrF); Serial.println(addrFc); float x= bytes2Float(fbs); for(int i=0;i<4;i++){ byte bf1=*((byte*)(addrF + i)); *((byte*)(addrFc + i))=bf1; Serial.print(bf1,HEX); if(i<3) Serial.print("-"); } Serial.println(" "); for(int j=0;j<4;j++){ Serial.print(fbs[j],HEX); if(j<3)Serial.print("-"); } Serial.println(" "); Serial.print("c:"); Serial.println(c,6); Serial.print("x:"); Serial.println(x,6); Serial.println(sizeof(b));

void float2Bytes(byte bytes_temp[4],float float_variable){ union { float a; byte bytes[4]; } thing; thing.a = float_variable; memcpy(bytes_temp, thing.bytes, 4); } float bytes2Float(byte bytes_temp[4]){ union { float a; byte bytes[4]; } thing; memcpy(thing.bytes,bytes_temp, 4); return thing.a; } //查找字节数组中是否以指定的字符串开头 boolean bStartsWith(byte data[],int len, String fStr){ int fLen=fStr.length() ; byte fBytes[fLen + 1]; fStr.getBytes(fBytes,fLen+1); if(len<=0)return false; if(len<fLen)return false; byte flag=1; for(int j=0;j<fLen;j++){ if(fBytes[j]!=data[j]) { flag=0; break; } } if(flag) return true; return false; } void long2byte(unsigned long res,byte targets[] ) { targets[0] = (byte) (res & 0xff); targets[1] = (byte) ((res >> 8) & 0xff); targets[2] = (byte) ((res >> 16) & 0xff); targets[3] = (byte) (res >> 24); } unsigned long bytes2long(byte buf[]) { unsigned long firstByte = 0; unsigned long secondByte = 0; unsigned long thirdByte = 0; unsigned long fourthByte = 0; int index = 0; firstByte = (0x000000FFU & ( buf[index+3])); secondByte = (0x000000FFU & ( buf[index + 2])); thirdByte = (0x000000FFU & ( buf[index + 1])); fourthByte = (0x000000FFU & ( buf[index ])); index = index + 4; return ((unsigned long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFUL; }
int array[13];
int * addrArr=array; //数组可以看成int类型的地址
arduino 发送float类型数据
float b=(float)millis() / 1000.0;
Serial2.write( ((byte*) &b),4);
//Serial2.flush();
delay(100);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
2015-04-14 FreeTextBox备忘