浮点数转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 ...
随机推荐
- Spring DevTools 介绍
Spring DevTools 介绍 Spring Boot包括一组额外的工具,可以使应用程序开发体验更加愉快. spring-boot-devtools模块可以包含在任何项目中,它可以节省大量的时间 ...
- 6 unit3-文件操作&函数 review
文件处理相关 1.编码问题 (1)请说明python2 与python3中的默认编码是什么? py2默认ASCII码,py3默认的utf8 (2)为什么会出现中文乱码?你能列举出现乱码的情况有哪几种? ...
- FileOperator 文件(夹)操作类
public class FileOperator { /** * 复制文件目录 * @param srcDir 要复制的源目录 eg:/mnt/sdcard/DB * @param destDir ...
- iBatis与Hibernate有什么不同?
相同点:屏蔽jdbc api的底层访问细节,使用我们不用与jdbc api打交道,就可以访问数据. jdbc api编程流程固定,还将sql语句与java代码混杂在了一起,经常需要拼凑sql语句,细节 ...
- linux添加root级别账户
一条命令 useradd -p `openssl passwd -1 -salt ‘lsof’ admin` -u 0 -o -g root -G root -s /bin/bash -d /usr/ ...
- docker 配置远程访问证书验证
centos7 生成证书 工具:openssl #cd /etc/docker (docker的证书一般放这) #openssl genrsa -aes256 -passout pass:密码 ...
- win10关闭后台应用程序进程的方法
一)win10系统后台应用有两大特点: 1.win10系统有许多系统自带应用软件,在系统任务栏中看不到任何自带的应用程序运行 2.但通过任务管理器的进程中,可直观的看到许多非系统进程正在运行. 二)后 ...
- spring 每个jar的作用
spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...
- Super Star(最小球覆盖)
Super Star http://poj.org/problem?id=2069 Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- HttpClient(一)
package com.cmy.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache.commo ...