转载自他人------byte[]与float[]互转
来源:互联网 发布:下载android源码 编辑:程序博客网 时间:2024/04/30 16:30
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] a = float2byte(1.3f);
System.out.println("****float 2 byte********"+a);
float aa = byte2float(a,0);-----------注意这里传0
System.out.println("****byte 2 float********"+aa);
}
/**
* 浮点转换为字节
*
* @param f
* @return
*/
public static byte[] float2byte(float f) {
// 把float转换为byte[]
int fbit = Float.floatToIntBits(f);
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (fbit >> (24 - i * 8));
}
// 翻转数组
int len = b.length;
// 建立一个与源数组元素类型相同的数组
byte[] dest = new byte[len];
// 为了防止修改源数组,将源数组拷贝一份副本
System.arraycopy(b, 0, dest, 0, len);
byte temp;
// 将顺位第i个与倒数第i个交换
for (int i = 0; i < len / 2; ++i) {
temp = dest[i];
dest[i] = dest[len - i - 1];
dest[len - i - 1] = temp;
}
return dest;
}
/**
* 字节转换为浮点
*
* @param b 字节(至少4个字节)
* @param index 开始位置
* @return
*/
public static float byte2float(byte[] b, int index) {
int l;
l = b[index + 0];
l &= 0xff;
l |= ((long) b[index + 1] << 8);
l &= 0xffff;
l |= ((long) b[index + 2] << 16);
l &= 0xffffff;
l |= ((long) b[index + 3] << 24);
return Float.intBitsToFloat(l);
}
0 0
- 转载自他人------byte[]与float[]互转
- java中float与byte[]互转
- 很多有用的算法--转载自他人
- C# float与Byte[]互换
- java byte 与 int float转换
- 表格的创建选项与及删除,转载他人
- C# double float int string 与 byte数组 相互转化
- C# 实现byte 与float 之间的转换
- C# double float int string 与 byte数组 相互转化
- C# double float int string 与 byte数组 相互转化
- C# double float int string 与 byte数组 相互转化
- java float/double转化byte[] 与C++端接收
- java中float与byte[]的互转
- java byte[] 与int float long 等的互相转换
- java中float与byte[]的互转
- byte[]与short、int、long、float、double的互换
- vc++ float to byte[]
- Bit与Byte的区别(转载)
- leetcode-implement-queue-using-stacks
- NSString什么时候用copy,什么时候用strong
- Android优秀开发资源
- 【设置】沉浸式状态栏、导航栏
- appRunning() 判断指定应用是否正在运行
- 转载自他人------byte[]与float[]互转
- 类与类之间的关系
- sphinx增量索引
- TF-IDF及其算法
- 程序员的核心竞争力是什么
- Java创建二叉搜索树,实现搜索,插入,删除操作
- UIImagePickerController 的部分用法详解
- ViewPager中切换及滑动切换Fragment数据保存状态讲解
- pascal vijos P1211-生日日数