1. 程式人生 > >各種位元組轉換為byte(ConvertCodeUtility)工具類

各種位元組轉換為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陣列

 
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 轉化成 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);

   }
   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陣列

 
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;
}

這裡這裡注意 我將unicode 解析字串做了位置互動 為了防止亂碼