最近跳槽到通信公司 各种解析,传递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数组转换为intpublic 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数组private static byte[] get7Bit(String strContent) { // 结果 byte[] arrResult = null; try { // 编码方式 byte[] arrs = strContent.getBytes("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) { intRight = -1; 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 转化成 stringpublic 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); } return sb.toString(); }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) { rs +=(char)b; } } return rs; } public static byte[] stringToBCD2(String str){ byte[] rs=new byte[15]; //str="460008453123160"; char[] c=str.toCharArray(); 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 = Integer.toHexString(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); } return unicode.toString(); }16 为上面unicode 解析string 转化为byte数组这里这里注意 我将unicode 解析字符串做了位置交互 为了防止乱码public static byte[] hexString2Bytes(String hex) { if ((hex == null) || (hex.equals(""))){ return null; } else if (hex.length()%2 != 0){ return null; } else{ hex = hex.toUpperCase(); int len = hex.length()/2; byte[] b = new byte[len]; char[] hc = hex.toCharArray(); 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; }