各种字节转换为byte(ConvertCodeUtility)⼯具类最近跳槽到通信公司  各种解析,传递byte数组  整理下转换为字节⼯具类  ⽅便下以后开发;
1.截取byte数组
public static byte[] subByteArr(byte[] data, int start, int length) {
if (length<=0) {
return new byte[0];
}
byte[] value = new byte[length];
if (data.length - start >= length) {
System.arraycopy(data, start, value, 0, length);
}
return value;
}
2.byte数组类型转换为short型
public static short bytes2Short(byte[] data) {
short value = (short) ((data[1] & 0xFF) | ((data[0] & 0xFF) << 8));
return value;
}
3.short转换为byte数组
public static byte[] short2Bytes(short value) {
byte[] data = new byte[2];
data[0] = (byte) (value >> 8 & 0xff);
data[1] = (byte) (value & 0xFF);
return data;
}
4.将 byte数组转换为int
public static int bytesToInt2(byte[] src) { // ⾼位在前,低位在后
int value;
value = (int) (((src[0] & 0xFF) << 24) | ((src[01] & 0xFF) << 16)
| ((src[02] & 0xFF) << 8) | (src[03] & 0xFF));
return value;
}
5.将 int 转换为 byte 数组
public static byte[] int2Bytes(int value) {
byte[] data = new byte[4];
data[0] = (byte) (value >> 24);
data[1] = (byte) (value >> 16 & 0xFF);
data[2] = (byte) (value >> 8 & 0xFF);
data[3] = (byte) (value & 0xFF);
return data;
}
6.合并 bytes 数组
public static byte[] mergeByteArray(byte[]... args) {
int length = 0;
int offset = 0;
for (byte[] arg : args) {
length += arg.length;
}
byte[] retVal = new byte[length];
for (byte[] arg : args) {
System.arraycopy(arg, 0, retVal, offset, arg.length);
offset += arg.length;
}
return retVal;
}
7.字符串转化为byte数组
public static byte[] stringToByteArray(String s, int length) {
byte[] retVal = null;
retVal = s.getBytes();
int left = length - retVal.length;
if (left > 0) {
for (int i = 0; i < left; i++) {
retVal = ConvertCodeUtility.AppendByte(retVal, (byte) 0);      }
}
return retVal;
}
8.数组合并
public static byte[] AppendByte(byte[] bytes, byte b) {
return mergeByteArray(bytes, new byte[] { b });
}
9.按照ASCAll解析的成byte数组
unicode在线工具
private static byte[] get7Bit(String strContent) {
// 结果
byte[] arrResult = null;
try {
// 编码⽅式
byte[] arrs = Bytes("ASCII");
System.out.println(new String(arrs));
arrResult = new byte[arrs.length - (arrs.length / 8)];
int intRight = 0;
int intLeft = 7;
int intIndex = 0;
for (int i = 1; i <= arrs.length; i++, intRight++, intLeft--) {
if (i % 8 == 0) {
intLeft = 8;
continue;
}
byte newItem = 0;
if (i == arrs.length) {
newItem = (byte) (arrs[i - 1] >> intRight);
} else {
newItem = (byte) ((arrs[i - 1] >> intRight) | (arrs[i] << intLeft));
}
arrResult[intIndex] = newItem;
intIndex++;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return arrResult;
}
10.bcd 转化成 string
public static String bcd2String(byte[] b) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
int l = (b[i] & 0x0f) + 48;
sb.append((char) l);
int h = ((b[i] & 0xff) >> 4) + 48;
sb.append((char) h);
}
String();
}
11.byte数组单纯转化为string类型
public static String byteArrayToHex(byte[] byteArray) {
// ⾸先初始化⼀个字符数组,⽤来存放每个16进制字符
char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
// new⼀个字符数组,这个就是⽤来组成结果字符串的(解释⼀下:⼀个byte是⼋位⼆进制,也就是2位⼗六进制字符(2的8次⽅等于16的2次⽅))char[] resultCharArray = new char[byteArray.length * 2];
// 遍历字节数组,通过位运算(位运算效率⾼),转换成字符放到字符数组中去
int index = 0;
for (byte b : byteArray) {
resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
resultCharArray[index++] = hexDigits[b & 0xf];
}
// 字符数组组合成字符串返回
return new String(resultCharArray);
}
12 字符串转化为bcd 码 byte数组
public static String getValidBytes(byte[] byteArray){
String rs="";
for (byte b:byteArray) {
if (b!=0) {
}
}
return rs;
}
public static byte[] stringToBCD2(String str){
byte[] rs=new byte[15];
//str="460008453123160";
char[] CharArray();
for (int i = 0; i < str.length(); i++) {
short temp = Short.parseShort(str.substring(i,i+1));
byte v = (byte)temp;
rs[i] = v;
}
return rs;
}
13.将⼀个byte 按照bit为输出
public static byte[] getBitArray(byte b) {
byte[] array = new byte[8];
for (int i = 7; i >= 0; i--) {
array[i] = (byte)(b & 1);
b = (byte) (b >> 1);
}
return array;
}
14 .将中⽂ string字符串转化为 unicode编码string字符串
public static String string2Unicode1(String string) {
StringBuffer unicode = new StringBuffer();
byte[] retVal = new byte[80];
for (int i = 0; i < string.length(); i++) {
// 取出每⼀个字符
char c = string.charAt(i);
String s = HexString(c);
if (s.length()==2){
s="00"+s;
}
String s1 = s.substring(0, 2);
String  s2 =s.substring(2,4);
s=s2+s1;
unicode.append(s);
}
String();
}
16 为上⾯unicode 解析string 转化为byte数组
public static byte[] hexString2Bytes(String hex) {
if ((hex == null) || (hex.equals(""))){
return null;
}
else if (hex.length()%2 != 0){
return null;
}
else{
hex = UpperCase();
int len = hex.length()/2;
byte[] b = new byte[len];
char[] hc = CharArray();
for (int i=0; i<len; i++){
int p=2*i;
b[i] = (byte) (toByte(hc[p]) << 4 | toByte(hc[p+1]));
}
return b;
}
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
这⾥这⾥注意我将unicode 解析字符串做了位置交互为了防⽌乱码

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。