java long类型、short类型与byte类型互转
<h2>java long类型、short类型与byte类型互转</h2>
<pre><code class="language-java">package com.spt.chinamobile.utils;
public class ByteUtil {
/**
* byte 转无符号 int
* @param a
* @return
*/
public int byte2UnsignedInt(byte a){
return a & 0xff;
}
//20000000001
public static long byte2long(byte[] b){
long result =0 ;
long temp = 0 ;
int len = b.length ;
for (int i = 0; i < len; i++) {
temp = (long)(b[i]&0xff)<<(len-1-i)*8 ;
result |=temp ;
}
return result ;
}
public static byte[] long2byte(long param,int len){
byte[] b = new byte[len] ;
for (int i = 0; i < len; i++) {
b[i] = (byte) ((param >> 8 * (len - i - 1)) & 0xff);
// LogUtil.d("ByteUtil",""+b[i]+",");
}
return b;
}
/**
* @功能 短整型与字节的转换,低8位在前,高8位在后
* @param 短整型
* @return 两位的字节数组
*/
public static byte[] short2byte(short number) {
int temp = number;
byte[] b = new byte[2];
for (int i = 0; i < b.length; i++) {
b[i] = new Integer(temp & 0xff).byteValue();// 得到低8位的值
temp = temp >> 8; // 向右移8位,得到高8位的值
}
return b;
}
//number>>8&0xff 得到高8位的值
//number&0xff 得到低8位的值
/**
* @功能 字节的转换与短整型
* @param 两位的字节数组
* @return 短整型
*/
public static short byte2short(byte[] b) {
short s = 0;
short s0 = (short) (b[0] & 0xff);// 最低位
short s1 = (short) (b[1] & 0xff);
s1 <<= 8;
s = (short) (s0 | s1);
return s;
}
public static int byteArrayToInt(byte[] valueBuf, int offset) {
ByteBuffer converter = ByteBuffer.wrap(valueBuf);
converter.order(ByteOrder.nativeOrder());
return converter.getInt(offset);
}
public static byte[] intToByteArray(int value) {
ByteBuffer converter = ByteBuffer.allocate(4);
converter.order(ByteOrder.nativeOrder());
converter.putInt(value);
return converter.array();
}
public static short byteArrayToShort(byte[] valueBuf, int offset) {
ByteBuffer converter = ByteBuffer.wrap(valueBuf);
converter.order(ByteOrder.nativeOrder());
return converter.getShort(offset);
}
public static byte[] shortToByteArray(short value) {
ByteBuffer converter = ByteBuffer.allocate(2);
converter.order(ByteOrder.nativeOrder());
short sValue = (short) value;
converter.putShort(sValue);
return converter.array();
}
/**
* Unicode码 转字符串
* @param unicode
* @return
*/
public static String unicodeToCn(String unicode) {
/** 以 \ u 分割,因为java注释也能识别unicode,因此中间加了一个空格*/
String[] strs = unicode.split("\\\\u");
String returnStr = "";
// 由于unicode字符串以 \ u 开头,因此分割出的第一个字符是""。
for (int i = 1; i < strs.length; i++) {
//returnStr += "\\u" + Integer.toString(chars[i], 16);
returnStr += "\\u" + Integer.toHexString(chars[i]);
}
return returnStr;
}
/**
* 字符创转Unicode码
* @param cn
* @return
*/
public static String cnToUnicode(String cn) {
char[] chars = cn.toCharArray();
System.out.println("chars.length = " + chars.length);
String returnStr = "";
for (int i = 0; i < chars.length; i++) {
//returnStr += "\\u" + Integer.toString(chars[i], 16);
returnStr += "\\u" + Integer.toHexString(chars[i]);
}
return returnStr;
}
}
/**
* 将字符串转化为 unicode码字节
* @param cn
* @return
*/
public static byte[] cnToByteArray(String cn){
String unicodeString = cnToUnicode(cn);
System.out.println("unicodeString = " + unicodeString);
String[] hexs = unicodeString.split("\\\\u");
for (int i = 0; i < hexs.length; i++) {
System.out.println("hexs["+i+"] = " + hexs[i]+" ");
}
ByteBuffer byteBuffer = ByteBuffer.allocate((hexs.length-1)*2);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
for (int i = 1; i < hexs.length; i++) {
int value = Integer.valueOf(hexs[i],16).intValue();
// byteBuffer.putShort((short)value);
byteBuffer.put((byte) (value>>8));//高8位
byteBuffer.put((byte) (value & 0xFF));//低8位
}
String revertCn = unicodeToCn(unicodeString);
System.out.println("revertCn = " + revertCn);
String returnUnicode = "";
int capacity = byteBuffer.capacity()/2;
System.out.println("to mcu datas: " + Arrays.toString(byteBuffer.array()));
for (int i = 0; i < capacity; i++) {
// returnUnicode += "\\u" + Integer.toString((byteBuffer.get(2*i)<<8|(byteBuffer.get(2*i+1)&0xff))&0xffff, 16);
returnUnicode += "\\u" + Integer.toHexString((byteBuffer.get(2*i)<<8|(byteBuffer.get(2*i+1)&0xff)) & 0xffff);
}
System.out.println("returnUnicode = " + returnUnicode);
String revert = unicodeToCn(returnUnicode);
System.out.println("revert = " + revert);
return byteBuffer.array();
}
</code></pre>