【转】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 ...
随机推荐
- AFNetworking (3.1.0) 源码解析 <四>
这次主要看一下文件夹Security中的类AFSecurityPolicy----安全策略类. AFSecurityPolicy主要的作用是验证HTTPS请求证书的有效性,在iOS9之后,默认不能发送 ...
- BaseAdapter的ArrayIndexOutOfBoundsException
最近写一个listView中多个listItem布局时,convertView缓存及使用,类似微信的聊天界面的listView,报了一个异常: 11-25 15:51:49.076: E/InputE ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- mysql中如何更新一个字段的值为它本身的值连接上一个字符串
CONCAT(str1,str2,...) 返回结果为连接参数产生的字符串. 如有任何一个参数为NULL ,则返回值为 NULL. 或许有一个或多个参数. 如果所有参数均为非二进制字符串,则结 ...
- Hadoop安装测试简单记录
安装的节点如下:1个namenode.1个hiveserver.3个dataNode192.168.1.139 namenode1192.168.1.146 hiveserver 192.16 ...
- mui实现自动登录
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name= ...
- codevs 4827 妹子
/* 赤裸裸的数学题 各种整体+插空 所以嘛 学好数学还是很重要的 ans=(n-1)!*(m-1)!*(2+(n-2)(n-1)) */ #include<iostream> #incl ...
- ASP.NET-FineUI开发实践-9(四)
现在是这么个问题,在开发中表格是动态出来的,就是标准板是全部字段列出,客户要根据情况列出自己想要的,在增加操作页面的同时要是能用前台自带的功能直接保存到后台就好了,现在的列显示和隐藏是不回发的. 1. ...
- id名和class名有什么区别
从概念上来说: id是先找到结构/内容,再给它定义样式: class是先定义好一种样式,再套给多个结构/内容. 从样式效果上来说: id的优先级要比class高出一个层次 html中不管有几个i ...
- C++线性序列容器<vector>简单总结
C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除 ...