浮点数转byte数组
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);
浮点数转byte数组的更多相关文章
- int跟byte[]数组互转的方法,整数 + 浮点型
整数: int转byte数组 public static byte[] intToBytes2(int n){ ]; ;i < ;i++) { b[i]=(-i*)); } return b; ...
- go语言:多个[]byte数组合并成一个[]byte
场景:在开发中,要将多个[]byte数组合并成一个[]byte,初步实现思路如下: 1.获取多个[]byte长度 2.构造一个二维码数组 3.循环将[]byte拷贝到二维数组中 package gst ...
- byte数组和File,InputStream互转
1.将File.FileInputStream 转换为byte数组: File file = new File("file.txt"); InputStream input = n ...
- C# byte数组与Image的相互转换
功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ...
- 透过byte数组简单分析Java序列化、Kryo、ProtoBuf序列化
序列化在高性能网络编程.分布式系统开发中是举足轻重的之前有用过Java序列化.ProtocolBuffer等,在这篇文章这里中简单分析序列化后的byte数组观察各种序列化的差异与性能,这里主要分析Ja ...
- 字符串与byte数组转换
string weclome=""; byte[] data = new byte[1024]; //字符串转byte数组 data = Encoding.ASCII.GetByt ...
- C# 将文件转化成byte[]数组
/// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl"& ...
- Java 文件和byte数组转换
/** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...
- InputStream,BufferedImage与byte数组之间的转换
需要获取网络的一张图片,但是某种需要,要把获取的这段流输入换为BufferedImage流,有的地方还需要转换为byte[]. 获得图片地址,获得了一个图片输入流,例如: Url img = n ...
随机推荐
- 编写一个基于Soap DataModule的三层数据库应用
服务器端:建立一个Web App Debugger executable类型,不需要接口,添加一个SoapData Module,放上AdoCon,AdoDataSet,DataSetProvider ...
- FMX TListView 搜索 Search
FMX,FireMonkey,平台框架下TListView控件搜索.过滤. 查找功能确实强大!! 设置SearchVisibe属性为true,就自动显示搜索框,输入文字,就开始检索了,不用写一行代码! ...
- JVM jstat 详解
http://www.ityouknow.com/java/2017/03/01/jvm-overview.html 本文基于JDK1.8 https://blog.csdn.net/maosijun ...
- 数学公式 AS3应用
普通做法: var pA:Point=new Point(100,100); var pB:Point=new Point(300,200); var dx:Number=pA.x-pB.x; var ...
- pycharm专业版(window)安装
1.官网下载 2. 3.直接finlsh 4. 5. https://pan.baidu.com/s/1mQcc98iJS5bnIyrC6097yA 密码:b1c1
- UI5-学习篇-2-Hello World
创建Application Project 1.打开Eclipse,创建Project sap.ui.commons 和 sap.m 是两个不同的 UI 库,但现在因为跨平台的原因,sap.ui.co ...
- python2限制函数传入的关键字参数
在Python2 中,可以通过使用**kwargs,在函数中配合使用kwargs.pop(key, False)实现获取限制关键字参数值,如果未传入则设置默认值,当所有需要的关键字参数都pop完毕,如 ...
- python的map函数和reduce函数(转)
map函数 map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例 ...
- Syncthing搭建
现在貌似只有windows和linux比较号装. 安装 先从官网下载好Windows 32位版(我本本对应的系统版本)的Syncthing,解压后可以看到如下文件结构 Syncthing文件结构 ...
- jquery clone 获取文本框值得问题
1 clone 出来的文本框 默认不会把原来的事件也带过去 如果使用 $("#").clone(true); true 可以将原来的事件带过去 获取文本框的值 可以使用事件 ...