原文网址: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的更多相关文章

  1. JAVA实体类不要使用基本类型,基本类型包含byte、int、short、long、float、double、char、boolean

    由于JAVA的基本类型会有默认值,例如当某个类中存在private  int age;字段时,创建这个类时,age会有默认值0.当使用age属性时,它总会有值.因此在某些情况下,便无法实现age为nu ...

  2. 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 ...

  3. java中String,int,Integer,char、double类型转换

    java中String,int,Integer,char.double类型转换----https://www.cnblogs.com/kangyu222/p/5866025.html

  4. C/C++中各种类型int、long、double、char表示范围(最大最小值)(转)

    #include<iostream> #include<string> #include <limits> using namespace std; int mai ...

  5. C/C++各种类型int、long、double、char表示范围(最大和最小)

    #include<iostream> #include<string> #include <limits> using namespace std; int mai ...

  6. java 金额计算,商业计算 double不精确问题 BigDecimal,Double保留两位小数方法

    解决办法================== http://blog.javaxxz.com/?p=763 一提到Java里面的商业计算,我们都知道不能用float和double,因为他们无法 进行精 ...

  7. 假设result 是一个float型变量,value是一个int型变量。执行以下赋值语句以后,变量value将是什么类型?为什么?

    假设result 是一个float型变量,value是一个int型变量.执行以下赋值语句以后,变量value将是什么类型?为什么? 在执行这条语句的过程中,保存在vulue变量中的值被读取出来并转化为 ...

  8. 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 ...

  9. 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 ...

  10. 【C++】int转string,double转string方法,string转int,string转double方法

    C++的格式比较多比较复杂,转换起来有很多方法,我这里只提供一种,仅供参考. int或double转string 使用字符串流的方式可以比较简单的完成转换 需要添加头文件 #include <s ...

随机推荐

  1. IE的CSS相关的BUG(整理一)

    本来不想弄这个ie的bug的,真的很想让它快点死掉,可是事与愿违啊,没办法,还是贴出来,以备自用. 这个网页(http://haslayout.net/css/index)上例举了所有的IE和CSS相 ...

  2. Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 编译lua版本问题

    Compile++ thumb  : game_shared <= main.cppjni/hellocpp/main.cpp: In function 'void Java_org_cocos ...

  4. make文件中静态连接库在command里面的位置

    linux软件第一次调用动态连接库的时候要延迟几秒,怀疑在突然掉库的时候动态库加载耗费了时间,所以造成延时,遂改为静态库方式,原来的makefile文件是 testmac:         cp $( ...

  5. 多表关联查询(ORACLE版)

    前言:这几天学习oracle,把自己对于关联查询的理解,记录下.如有错误请指正! 交叉连接: 交欢连接又称为“笛卡儿积连接”,是两个或多个表之间的无条件连接.一个表中所有的记录与其它表的所有的记录进行 ...

  6. 【Android】无限滚动的HorizontalScrollView

    这是一个很简单的功能,作为新手,做一下笔记.也给其它有需要的人提供一个参考. 首先HorizontalScrollView都知道用途了.它可以实现类似“桌面程序”左右切换页的效果.一般情况下里面的页数 ...

  7. Linux下multipath多路径配置

    一.什么是多路径 普通的电脑主机都是一个硬盘挂接到一个总线上,这里是一对一的关系.而到了有光纤组成的SAN环境,或者由iSCSI组成的IPSAN环境,由于主机和存 储通过了光纤交换机或者多块网卡及IP ...

  8. SQL 去除小数点后无效 0 的方法

     select convert(float,10.0000)  就是这么简单

  9. HTML5和CSS3实例教程[总结一]

    关于onclick的行为与内容分离 通过链接触发弹出窗口方式 (不推荐使用此方法!!!) <a href='#' onclcik = "window.open('holiday_pay ...

  10. hdu 1728

    //hdu 1728 //这个是一道很经典的迷宫题了,思路感觉...取起点和终点,判断连线是否超过n个弯, //先是从起点出发,上下左右四个方向搜索,找到一条路,把那条路的第一个点压入队列 //然后沿 ...