byte数组与int,long,short,byte转换 (转载)
byte数组和short数组转换
public short bytesToShort(byte[] bytes) {
return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
}
public byte[] shortToBytes(short value) {
return ByteBuffer.allocate().order(ByteOrder.LITTLE_ENDIAN).putShort(value).array();
}
public short[] byteArray2ShortArray(byte[] data, int items) {
short[] retVal = new short[items];
for (int i = 0; i < retVal.length; i++)
retVal[i] = (short) ((data[i * 2] & 0xff) | (data[i * 2 + 1] & 0xff) << 8); return retVal;
}
public class DataTypeChangeHelper {
/**
* 将一个单字节的byte转换成32位的int
*
* @param b
* byte
* @return convert result
*/
public static int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
} /**
* 将一个单字节的Byte转换成十六进制的数
*
* @param b
* byte
* @return convert result
*/
public static String byteToHex(byte b) {
int i = b & 0xFF;
return Integer.toHexString(i);
} /**
* 将一个4byte的数组转换成32位的int
*
* @param buf
* bytes buffer
* @param byte[]中开始转换的位置
* @return convert result
*/
public static long unsigned4BytesToInt(byte[] buf, int pos) {
int firstByte = 0;
int secondByte = 0;
int thirdByte = 0;
int fourthByte = 0;
int index = pos;
firstByte = (0x000000FF & ((int) buf[index]));
secondByte = (0x000000FF & ((int) buf[index + 1]));
thirdByte = (0x000000FF & ((int) buf[index + 2]));
fourthByte = (0x000000FF & ((int) buf[index + 3]));
index = index + 4;
return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
} /**
* 将16位的short转换成byte数组
*
* @param s
* short
* @return byte[] 长度为2
* */
public static byte[] shortToByteArray(short s) {
byte[] targets = new byte[2];
for (int i = 0; i < 2; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
} /**
* 将32位整数转换成长度为4的byte数组
*
* @param s
* int
* @return byte[]
* */
public static byte[] intToByteArray(int s) {
byte[] targets = new byte[2];
for (int i = 0; i < 4; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
} /**
* long to byte[]
*
* @param s
* long
* @return byte[]
* */
public static byte[] longToByteArray(long s) {
byte[] targets = new byte[2];
for (int i = 0; i < 8; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
} /**32位int转byte[]*/
public static byte[] int2byte(int res) {
byte[] targets = new byte[4];
targets[0] = (byte) (res & 0xff);// 最低位
targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
return targets;
} /**
* 将长度为2的byte数组转换为16位int
*
* @param res
* byte[]
* @return int
* */
public static int byte2int(byte[] res) {
// res = InversionByte(res);
// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
return targets;
}
}
byte数组与int,long,short,byte转换 (转载)的更多相关文章
- 【转】java中byte数组与int类型的转换(两种方式)----不错
原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...
- Java 中 byte、byte 数组和 int、long 之间的转换
Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...
- byte数组和int之间相互转化的方法
Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...
- [转载] java中byte数组与int,long,short间的转换
文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...
- java byte数组与int,long,short,byte转换
public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return conver ...
- java中byte数组与int,long,short间的转换
http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...
- byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化
我们先来看看byte bool int ushort 等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...
- byte[]数组和int之间的转换
这里简单记录下两种转换方式: 第一种: 1.int与byte[]之间的转换(类似的byte short,long型) /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高 ...
- Byte数组和Int的互相转换
public static int bytesToInt(byte[] bytes) { int addr = bytes[0] & 0xFF; addr |= ((bytes[1] < ...
随机推荐
- ZS and The Birthday Paradox
ZS and The Birthday Paradox 题目链接:http://codeforces.com/contest/711/problem/E 数学题(Legendre's formula) ...
- MVC运行机制
一,第一次程序运行时 1,第一次请求的时候 会获取配置文件,然后有个应用启动事件到global.asax.2,在Global.asax文件中,网站第一次运行会创建RouteTable对象,实现URL到 ...
- 火狐中,设置align="center"失效的解决方法
如下: <img align="center" style="vertical-align:top"></img> 只需要加上: ver ...
- JavaEE JavaBean 反射、内省、BeanUtils
JavaEE JavaBean 反射.内省.BeanUtils @author ixenos JavaBean是什么 一种规范,表达实体和信息的规范,便于封装重用. 1.所有属性为private2.提 ...
- hdu_5963_朋友(找规律)
题目链接:hdu_5963_朋友 题意: 中文,不解释 题解: 把样例拿出来看看,你会发现以x为节点是否能赢,就是与x相连的边权值的和或者异或是否为奇数. #include<bits/stdc+ ...
- hdu_5790_Prefix(trie+主席树)
题目链接:hdu_5790_Prefix 题意: 给你n个字符串,字符串总长度不超过10W,然后给你一个区间,问你这个区间的字符串不相同的前缀有多少个. 题解: 由于z与上一个答案有关,所以强制在线, ...
- Objetive-C initialize研究
initialize执行时机 在向一个类执行实例方法或者类方法(除了initialize和load方法之外)的时候,触发initialize方法,因此如果一个类你没有使用的时候,是 ...
- openwrt启动过程(脚本)
来源: http://wiki.openwrt.org/doc/techref/preinit_mount#first.boot 基本的openwrt启动顺序为: 1.boot loader loa ...
- intellij中javax包的导入
新建intellij的maven web想项目的时候,出现的问题,import javax.servlet.http相关的包出错 好久不搭建也好久没用intellij了,都忘了,在这里笔记一下 再次吐 ...
- 第1章 初识java----输出多行的语句写法
public class onesixtwo{ public static void main(String[] args){ System.out.println("----------- ...