【转】java byte转long、double、float、int、short,或者long、double、float、int、short转byte
原文网址:http://www.xuebuyuan.com/988752.html
java byte与其他数据类型的转换主要用于二进制数据的编码和解码,主要用于网络传输,读写二进制文件,java和c++服务器之间的数据通信等等
以下是总结的源码
/**
* BYTE转INT
*
* @param b
* @return
*/
protected int byteArrayToInt(byte[] b) {
return (b[0] << 24) + ((b[1] & 0xFF) << 16) + ((b[2] & 0xFF) << 8)
+ (b[3] & 0xFF);
}
/**
* BYTE转SHORT
*
* @param b
* @return
*/
protected int byteArrayToShort(byte[] b) {
return (b[0] << 8) + (b[1] & 0xFF);
}
/**
* SHORT转BYTE数据
*
* @param s
* @return
*/
protected byte[] shortToByteArray(short s) {
byte[] shortBuf = new byte[2];
for (int i = 0; i < 2; i++) {
int offset = (shortBuf.length - 1 - i) * 8;
shortBuf[i] = (byte) ((s >>> offset) & 0xff);
}
return shortBuf;
}
/**
* INT数据转BYTE数据
*
* @param i
* @return
*/
protected byte[] intToByteArray(int i) {
byte[] result = new byte[4];
result[0] = (byte) ((i >> 24) & 0xFF);
result[1] = (byte) ((i >> 16) & 0xFF);
result[2] = (byte) ((i >> 8) & 0xFF);
result[3] = (byte) (i & 0xFF);
return result;
}
/**
* 转换long型为byte数组
*
* @param bb
* @param x
* @param index
*/
public byte[] longToByteArray(long x, int index) {
byte[] bb = new byte[8];
bb[index + 7] = (byte) (x >> 56);
bb[index + 6] = (byte) (x >> 48);
bb[index + 5] = (byte) (x >> 40);
bb[index + 4] = (byte) (x >> 32);
bb[index + 3] = (byte) (x >> 24);
bb[index + 2] = (byte) (x >> 16);
bb[index + 1] = (byte) (x >> 8);
bb[index + 0] = (byte) (x >> 0);
return bb;
}
/**
* 通过byte数组取到long
*
* @param bb
* @param index
* @return
*/
public long byteArrayToLong(byte[] bb, int index) {
return ((((long) bb[index + 7] & 0xff) << 56)
| (((long) bb[index + 6] & 0xff) << 48)
| (((long) bb[index + 5] & 0xff) << 40)
| (((long) bb[index + 4] & 0xff) << 32)
| (((long) bb[index + 3] & 0xff) << 24)
| (((long) bb[index + 2] & 0xff) << 16)
| (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));
}
/**
* float转换byte
*
* @param bb
* @param x
* @param index
*/
public static byte[] floatTobyteArray(float v) {
ByteBuffer bb = ByteBuffer.allocate(4);
byte[] ret = new byte[4];
FloatBuffer fb = bb.asFloatBuffer();
fb.put(v);
bb.get(ret);
return ret;
}
/**
* 通过byte数组取得float
*
* @param bb
* @param index
* @return
*/
public static float byteArrayToFloat(byte[] v) {
ByteBuffer bb = ByteBuffer.wrap(v);
FloatBuffer fb = bb.asFloatBuffer();
return fb.get();
}
/**
* double转换byte
*
* @param bb
* @param x
* @param index
*/
public byte[] doubleToByteArray(double x) {
ByteBuffer bb = ByteBuffer.allocate(8);
byte[] ret = new byte[8];
DoubleBuffer fb = bb.asDoubleBuffer();
fb.put(x);
bb.get(ret);
return ret;
}
/**
* 通过byte数组取得float
*
* @param bb
* @param index
* @return
*/
public double byteArrayToDouble(byte[] b) {
ByteBuffer bb = ByteBuffer.wrap(b);
DoubleBuffer fb = bb.asDoubleBuffer();
return fb.get();
}
【转】java byte转long、double、float、int、short,或者long、double、float、int、short转byte的更多相关文章
- JAVA实体类不要使用基本类型,基本类型包含byte、int、short、long、float、double、char、boolean
由于JAVA的基本类型会有默认值,例如当某个类中存在private int age;字段时,创建这个类时,age会有默认值0.当使用age属性时,它总会有值.因此在某些情况下,便无法实现age为nu ...
- 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语
数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...
- java中String,int,Integer,char、double类型转换
java中String,int,Integer,char.double类型转换----https://www.cnblogs.com/kangyu222/p/5866025.html
- C/C++中各种类型int、long、double、char表示范围(最大最小值)(转)
#include<iostream> #include<string> #include <limits> using namespace std; int mai ...
- C/C++各种类型int、long、double、char表示范围(最大和最小)
#include<iostream> #include<string> #include <limits> using namespace std; int mai ...
- java 金额计算,商业计算 double不精确问题 BigDecimal,Double保留两位小数方法
解决办法================== http://blog.javaxxz.com/?p=763 一提到Java里面的商业计算,我们都知道不能用float和double,因为他们无法 进行精 ...
- 假设result 是一个float型变量,value是一个int型变量。执行以下赋值语句以后,变量value将是什么类型?为什么?
假设result 是一个float型变量,value是一个int型变量.执行以下赋值语句以后,变量value将是什么类型?为什么? 在执行这条语句的过程中,保存在vulue变量中的值被读取出来并转化为 ...
- Number 强制类型转换 int 强制转换整型 float 强制转换浮点型 complex 强制转换成复数 bool 强制转换成布尔类型,结果只有两种,要么True 要么 False """bool 可以转换所有的数据类型 everything"""
# ###Number 强制类型转换 var1 = 5 var2 = 4.85 var3 = True var3_2 = False var4 = 3+9j var5 = "888777&q ...
- unity c# script error CS0664: Literal of type double cannot be implicitly converted to type `float'. Add suffix `f' to create a literal of this type
例如在unity c# script中定义 private float x=0.0; 则会报 error CS0664: Literal of type double cannot be implic ...
- 【C++】int转string,double转string方法,string转int,string转double方法
C++的格式比较多比较复杂,转换起来有很多方法,我这里只提供一种,仅供参考. int或double转string 使用字符串流的方式可以比较简单的完成转换 需要添加头文件 #include <s ...
随机推荐
- hdu5032 Always Cook Mushroom
题意是这样,给定一个1000x1000的点阵.m组询问.每次询问一个由(0,0).(x,0)点一以及从原点出发的方向向量(a,b)构成的直角三角形包围的点的权值和. 点的权值是(x+A)(y+B),当 ...
- java 中读取本地文件中字符
java读取txt文件内容.可以作如下理解: 首先获得一个文件句柄.File file = new File(); file即为文件句柄.两人之间连通电话网络了.接下来可以开始打电话了. 通过这条线路 ...
- swift开发笔记24 解决键盘遮挡输入框 的方法
很简单,就是开始输入时把整个view的frame上移,也就是把y值减小就行了,至于减少多少自己调 ,也可以动态获取参见(http://blog.csdn.net/lengshengren/articl ...
- AsMVC:一个简单的MVC框架的Java实现
当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍sp ...
- 内存泄露 Memory Leaks
什么是内存泄露 内存管理一直是Java 所鼓吹的强大优点.开发者只需要简单地创建对象,而Java的垃圾收集器将会自动管理内存空间的分配和释放. 但在很多情况下,事情并不那么简单,在 Java程序中总是 ...
- 帧动画 AnimationDrawable
Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果. 首先,在res/drawable中定义动画 < ...
- hdu 2304
题意: 插座插空问题 水题.....只要知道最后一个不需要插即可.... 直接贴代码.. AC代码: #include <iostream> using namespace std; in ...
- 引号 shell
在学些shell的 grep, awk, sed 中,发现<Linux 与Unix Shell 编程 指南>书中用大多都是单引号. 一开始我总在寻思,为什么用单引号,明明双引号也是行的呀. ...
- maven自动下载jar包
只需要修改pom文件即可.需要哪个jar包,在pom中就配置哪个(还包括手动向仓库中添加) 例如 http://blog.csdn.net/beyondlpf/article/details/8592 ...
- php随机密码
<?php /* * php自动生成新密码自定义函数(带实例演示) 适用环境: PHP5.2.x / mysql 5.0.x * */ function genPassword($a=1,$b= ...