android String byte[] 進制之間的互相轉換

/**
 * 字符串轉換成十六進制字符串
 * @param String str 待轉換的ASCII字符串
 * @return String 每一個Byte之間空格分隔,如: [61 6C 6B]
 */
public static String str2HexStr(String str)
{

   char[] chars = "0123456789ABCDEF".toCharArray();
   StringBuilder sb = new StringBuilder("");
   byte[] bs = str.getBytes();
   int bit;

   for (int i = 0; i < bs.length; i++)
   {
      bit = (bs[i] & 0x0f0) >> 4;
      sb.append(chars[bit]);
      bit = bs[i] & 0x0f;
      sb.append(chars[bit]);
      sb.append(' ');
   }
   return sb.toString().trim();
}

/**
 * 十六進制轉換字符串
 * @param String str Byte字符串(Byte之間無分隔符 如:[616C6B])
 * @return String 對應的字符串
 */
public static String hexStr2Str(String hexStr)
{
   String str = "0123456789ABCDEF";
   char[] hexs = hexStr.toCharArray();
   byte[] bytes = new byte[hexStr.length() / 2];
   int n;

   for (int i = 0; i < bytes.length; i++)
   {
      n = str.indexOf(hexs[2 * i]) * 16;
      n += str.indexOf(hexs[2 * i + 1]);
      bytes[i] = (byte) (n & 0xff);
   }
   return new String(bytes);
}

/**
 * bytes轉換成十六進制字符串
 * @param byte[] b byte數組
 * @return String 每一個Byte值之間空格分隔
 */
public static String byte2HexStr(byte[] b)
{
   String stmp="";
   StringBuilder sb = new StringBuilder("");
   for (int n=0;n<b.length;n++)
   {
      stmp = Integer.toHexString(b[n] & 0xFF);
      sb.append((stmp.length()==1)? "0"+stmp : stmp);
      sb.append(" ");
   }
   return sb.toString().toUpperCase().trim();
}

/**
 * bytes字符串轉換爲Byte * @param String src Byte字符串,每一個Byte之間沒有分隔符
 * @return byte[]
 */
public static byte[] hexStr2Bytes(String src)
{
   int m=0,n=0;
   int l=src.length()/2;
   System.out.println(l);
   byte[] ret = new byte[l];
   for (int i = 0; i < l; i++)
   {
      m=i*2+1;
      n=m+1;
      ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n));
   }
   return ret;
}

/**
 * String的字符串轉換成unicodeString
 * @param String strText 全角字符串
 * @return String 每一個unicode之間無分隔符
 * @throws Exception
 */
public static String strToUnicode(String strText)
      throws Exception
{
   char c;
   StringBuilder str = new StringBuilder();
   int intAsc;
   String strHex;
   for (int i = 0; i < strText.length(); i++)
   {
      c = strText.charAt(i);
      intAsc = (int) c;
      strHex = Integer.toHexString(intAsc);
      if (intAsc > 128)
         str.append("\\u" + strHex);
      else // 低位在前面補00
         str.append("\\u00" + strHex);
   }
   return str.toString();
}

/**
 * unicodeString轉換成String的字符串
 * @param String hex 16進制值字符串 (一個unicode2byte * @return String 全角字符串
 */
public static String unicodeToString(String hex)
{
   int t = hex.length() / 6;
   StringBuilder str = new StringBuilder();
   for (int i = 0; i < t; i++)
   {
      String s = hex.substring(i * 6, (i + 1) * 6);
      // 高位須要補上00再轉
      String s1 = s.substring(2, 4) + "00";
      // 低位直接轉
      String s2 = s.substring(4);
      // 16進制的string轉爲int
      int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
      // int轉換爲字符
      char[] chars = Character.toChars(n);
      str.append(new String(chars));
   }
   return str.toString();
}
//-------------------------------------------------------
// 判斷奇數或偶數,位運算,最後一位是1則爲奇數,爲0是偶數
static public int isOdd(int num)
{
   return num & 0x1;
}
//-------------------------------------------------------
static public int HexToInt(String inHex)//Hex字符串轉int
{
   return Integer.parseInt(inHex, 16);
}
//-------------------------------------------------------
static public byte HexToByte(String inHex)//Hex字符串轉byte
{
   return (byte)Integer.parseInt(inHex,16);
}
//-------------------------------------------------------
static public String Byte2Hex(Byte inByte)//1字節轉2Hex字符
{
   return String.format("%02x", inByte).toUpperCase();
}
//-------------------------------------------------------
static public String ByteArrToHex(byte[] inBytArr)//字節數組轉轉hex字符串
{
   StringBuilder strBuilder=new StringBuilder();
   int j=inBytArr.length;
   for (int i = 0; i < j; i++)
   {
      strBuilder.append(Byte2Hex(inBytArr[i]));
      strBuilder.append(" ");
   }
   return strBuilder.toString();
}
//-------------------------------------------------------
static public String ByteArrToHex(byte[] inBytArr,int offset,int byteCount)//字節數組轉轉hex字符串,可選長度
{
   StringBuilder strBuilder=new StringBuilder();
   int j=byteCount;
   for (int i = offset; i < j; i++)
   {
      strBuilder.append(Byte2Hex(inBytArr[i]));
   }
   return strBuilder.toString();
}
//-------------------------------------------------------
//hex字符串轉字節數組
static public byte[] HexToByteArr(String inHex)//hex字符串轉字節數組
{
   int hexlen = inHex.length();
   byte[] result;
   if (isOdd(hexlen)==1)
   {//奇數
      hexlen++;
      result = new byte[(hexlen/2)];
      inHex="0"+inHex;
   }else {//偶數
      result = new byte[(hexlen/2)];
   }
   int j=0;
   for (int i = 0; i < hexlen; i+=2)
   {
      result[j]=HexToByte(inHex.substring(i,i+2));
      j++;
   }
   return result;
}
相關文章
相關標籤/搜索