浮点数转byte数组
2024-08-26 20:03:15
long a=;
float b=34.56745f;
float c=0.0; byte fbs[]={,,,};
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=;i<;i++){
byte bf1=*((byte*)(addrF + i));
*((byte*)(addrFc + i))=bf1;
Serial.print(bf1,HEX);
if(i<) Serial.print("-");
}
Serial.println(" "); for(int j=;j<;j++){
Serial.print(fbs[j],HEX);
if(j<)Serial.print("-"); }
Serial.println(" ");
Serial.print("c:");
Serial.println(c,); Serial.print("x:");
Serial.println(x,); Serial.println(sizeof(b));
void float2Bytes(byte bytes_temp[],float float_variable){
union {
float a;
byte bytes[];
} thing;
thing.a = float_variable;
memcpy(bytes_temp, thing.bytes, );
} float bytes2Float(byte bytes_temp[]){
union {
float a;
byte bytes[];
} thing; memcpy(thing.bytes,bytes_temp, );
return thing.a;
} //查找字节数组中是否以指定的字符串开头
boolean bStartsWith(byte data[],int len, String fStr){
int fLen=fStr.length() ;
byte fBytes[fLen + ];
fStr.getBytes(fBytes,fLen+); if(len<=)return false;
if(len<fLen)return false;
byte flag=;
for(int j=;j<fLen;j++){
if(fBytes[j]!=data[j])
{
flag=;
break;
} }
if(flag) return true; return false;
} void long2byte(unsigned long res,byte targets[] ) {
targets[] = (byte) (res & 0xff);
targets[] = (byte) ((res >> ) & 0xff);
targets[] = (byte) ((res >> ) & 0xff);
targets[] = (byte) (res >> ); } unsigned long bytes2long(byte buf[]) {
unsigned long firstByte = ;
unsigned long secondByte = ;
unsigned long thirdByte = ;
unsigned long fourthByte = ;
int index = ;
firstByte = (0x000000FFU & ( buf[index+]));
secondByte = (0x000000FFU & ( buf[index + ]));
thirdByte = (0x000000FFU & ( buf[index + ]));
fourthByte = (0x000000FFU & ( buf[index ]));
index = index + ;
return ((unsigned long) (firstByte << | secondByte << | thirdByte << | 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);
最新文章
- codevs 1228 苹果树 树链剖分讲解
- yii2史上最简单式安装教程,没有之一
- 【Java源码分析】LinkedList类
- While reading xxx.png pngcrush caught libpng error: Not a PNG file..
- webpack打包sass
- java中关于移位运算符的demo与总结
- 微信--高效解决token及授权用户openid的持久化处理办法
- Bootstrap入门(二十三)JS插件1:模态框
- C语言获取系统时间的函数
- ArrayList源码分析超详细
- vue.js数据可以在页面上渲染成功却总是警告提示某个字段“undefined”未定义
- 201771010118《面向对象程序设计(java)》第三周学习总结
- RNP项目遇到的坑
- 【原创】大叔经验分享(36)CM部署kafka
- 建立标准编码规则(二)-DiagnosticAnalyzer 增加诊断分析代码
- Xgboost理解
- css3常用动画样式文件move.css
- JavaScript中的坑
- jq03--基础函数
- Python之路,Day2 - Python基础,列表,循环