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转换 (转载)的更多相关文章

  1. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

  2. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  3. byte数组和int之间相互转化的方法

    Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...

  4. [转载] java中byte数组与int,long,short间的转换

    文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...

  5. java byte数组与int,long,short,byte转换

    public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return conver ...

  6. java中byte数组与int,long,short间的转换

    http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...

  7. byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化

    我们先来看看byte bool  int ushort  等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...

  8. byte[]数组和int之间的转换

    这里简单记录下两种转换方式: 第一种: 1.int与byte[]之间的转换(类似的byte short,long型) /** * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高 ...

  9. Byte数组和Int的互相转换

    public static int bytesToInt(byte[] bytes) { int addr = bytes[0] & 0xFF; addr |= ((bytes[1] < ...

随机推荐

  1. win32api大全

    win32api大全 http://files.cnblogs.com/files/landv/Win32API%E5%A4%A7%E5%85%A8.zip

  2. CENTOS7小注意

    由于第一次使用 Linux CENTOS ,所以安装了图形界面,但是在终端执行yum 安装的时候,总是提示 Existing lock /var/run/yum.pid: another copy i ...

  3. git的使用,将本地项目push到github上

    Git教程(推荐): http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000git是先用a ...

  4. iOS开发富文本

    NSMutableAttributedString * attributedStr = [[NSMutableAttributedString alloc] initWithString:@" ...

  5. html 超链接(a)详细讲解

    a:link : http://www.cnblogs.com/yangfeng/archive/2009/07/25/1530962.html 超级链接 超级链接是网站中使用比较频繁的HTML元素, ...

  6. CSS3秘笈复习:第九章&第十章

    第九章 1.和链接有关的伪类: (1):link,未访问过的链接 (2):visited,已访问过的链接 (3):hover,鼠标悬停链接 (4):active,单击链接时 这四种方式一定要严格按上面 ...

  7. 6、Web应用程序中的安全向量 -- customErrors(适当的错误报告和堆栈跟踪)

    几乎所有的网站在开发过程中都在web.config文件中设置了特性<customErrors mode="off">. customErrors模式有3个可选的设置项: ...

  8. SecureCRT 7.3.4破解版(含注册机)

    不用说你肯定知道SecureCRT用途是什么,这个号称最好用的ssh连接工具却不是免费的,所以找了很久才找到最新版本的SecureCRT 7.3.4破解版,其实只要是SecureCRT 7.3.x版本 ...

  9. 如何使用自定义消息?--ESFramework 4.0 快速上手(04)

    在ESFramework 4.0 快速上手一文中,我们讲述了如何使用Rapid引擎可以快速地上手ESFramework开发,文中介绍了使用ESPlus.Application.CustomizeInf ...

  10. PHP中使用CURL(六)

    curl常用的几个例子 1.抓取无访问控制文件 <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://local ...