1. 程式人生 > >java中byte[]、buffer、string轉換

java中byte[]、buffer、string轉換

將原陣列資料拷貝到目的陣列

System.arraycopy(src, Srcposition, dst, DstPosition,dstLength);

將位元組陣列轉換成位元組緩衝區

ByteBuffer buffer = ByteBuffer.wrap(Bytes[]);

目前buffer存在的位置與buffer的容量

buffer.position() < buffer.capacity()

將buffer讀取到目的

buffer.get(dstBytes[], 0, length);

位元組轉string

public static String toString(byte[] buf, int offset, int length,
String charsetName) {
String str = null;
if (charsetName == null || charsetName.equals("")) {
charsetName = System.getProperty("file.encoding");
}
try {
str = new String(buf, offset, length, charsetName);
} catch (UnsupportedEncodingException e) {
str = new String(buf, offset, length);
}
return str;
}

位元組轉換成數值:

public static String toIp(byte[] buf, int offset, int length) {
StringBuilder stbuf = new StringBuilder();
;
for (int i = 0; i < length; i++) {
int value = buf[offset++] & 0xff;
stbuf.append(value).append(".");
}
stbuf.deleteCharAt(stbuf.length() - 1);
return stbuf.toString();
}

  1. /** 
  2.  * @Package:
     
  3.  * @ClassName:TypeConversion 
  4.  * @Description:位元組流、字串、16進位制字串轉換 
  5.  * @author:xk 
  6.  * @date:Jan 8, 2013 5:00:08 PM 
  7.  */  
  8. public class TypeConversion {  
  9.     /** 
  10.      * @Title:bytes2HexString 
  11.      * @Description:位元組陣列轉16進位制字串 
  12.      * @param b 
  13.      *            位元組陣列 
  14.      * @return 16進位制字串 
  15.      * @throws
     
  16.      */  
  17.     public static String bytes2HexString(byte[] b) {  
  18.         StringBuffer result = new StringBuffer();  
  19.         String hex;  
  20.         for (int i = 0; i < b.length; i++) {  
  21.             hex = Integer.toHexString(b[i] & 0xFF);  
  22.             if (hex.length() == 1) {  
  23.                 hex = '0' + hex;  
  24.             }  
  25.             result.append(hex.toUpperCase());  
  26.         }  
  27.         return result.toString();  
  28.     }  
  29.     /** 
  30.      * @Title:hexString2Bytes 
  31.      * @Description:16進位制字串轉位元組陣列 
  32.      * @param src 
  33.      *            16進位制字串 
  34.      * @return 位元組陣列 
  35.      * @throws 
  36.      */  
  37.     public static byte[] hexString2Bytes(String src) {  
  38.         int l = src.length() / 2;  
  39.         byte[] ret = new byte[l];  
  40.         for (int i = 0; i < l; i++) {  
  41.             ret[i] = (byte) Integer  
  42.                     .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();  
  43.         }  
  44.         return ret;  
  45.     }  
  46.     /** 
  47.      * @Title:string2HexString 
  48.      * @Description:字串轉16進位制字串 
  49.      * @param strPart 
  50.      *            字串 
  51.      * @return 16進位制字串 
  52.      * @throws 
  53.      */  
  54.     public static String string2HexString(String strPart) {  
  55.         StringBuffer hexString = new StringBuffer();  
  56.         for (int i = 0; i < strPart.length(); i++) {  
  57.             int ch = (int) strPart.charAt(i);  
  58.             String strHex = Integer.toHexString(ch);  
  59.             hexString.append(strHex);  
  60.         }  
  61.         return hexString.toString();  
  62.     }  
  63.     /** 
  64.      * @Title:hexString2String 
  65.      * @Description:16進位制字串轉字串 
  66.      * @param src 
  67.      *            16進位制字串 
  68.      * @return 位元組陣列 
  69.      * @throws 
  70.      */  
  71.     public static String hexString2String(String src) {  
  72.         String temp = "";  
  73.         for (int i = 0; i < src.length() / 2; i++) {  
  74.             temp = temp  
  75.                     + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),  
  76.                             16).byteValue();  
  77.         }  
  78.         return temp;  
  79.     }  
  80.     /** 
  81.      * @Title:char2Byte 
  82.      * @Description:字元轉成位元組資料char-->integer-->byte 
  83.      * @param src 
  84.      * @return 
  85.      * @throws 
  86.      */  
  87.     public static Byte char2Byte(Character src) {  
  88.         return Integer.valueOf((int)src).byteValue();  
  89.     }  
  90.         /** 
  91.      * @Title:intToHexString 
  92.      * @Description:10進位制數字轉成16進位制 
  93.      * @param a 轉化資料 
  94.      * @param len 佔用位元組數 
  95.      * @return 
  96.      * @throws 
  97.      */  
  98.     private static String intToHexString(int a,int len){  
  99.         len<<=1;  
  100.         String hexString = Integer.toHexString(a);  
  101.         int b = len -hexString.length();  
  102.         if(b>0){  
  103.             for(int i=0;i<b;i++)  {  
  104.                 hexString = "0" + hexString;  
  105.             }  
  106.         }  
  107.         return hexString;  
  108.     }  
  109.     public static void main(String args[]) {  
  110.         System.out.println(hexString2String("3133383131313536373838"));  
  111.     }  
  112. }