/**
* Split the string by two digits
*/
public static String splitTwo(String aa)
{
aa = aa.replaceAll(".{2}(?!$)", "$0 ");
aa = aa. replaceAll(" ", " 0x");
aa = "0x".concat(aa);
return aa;
}
/**
* bytes converted to hexadecimal string
* @param byte[] b byte array
* @return String is separated by spaces between each Byte value
*/
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 string converted to Byte value
* @param String src Byte string, no separator between each 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;
}
/**
* Convert unicode String to String String
* @param String hex hexadecimal value string (one unicode is 2byte)
* @return String full-width 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);
// The high bit needs to be filled with 00 and then converted to
String s1 = s.substring(2, 4) + "00";
// Convert the low order directly to
String s2 = s.substring(4);
// Convert hexadecimal string to int
int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
// convert int to character
char[] chars = Character.toChars(n);
str.append(new String(chars));
}
return str.toString();
}
/**
* String converted to unicode String
* @param String strText Full-width string
* @return String No separator between each 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 // low order first Add 00
str.append(" \\u00 " + strHex);
}
return str.toString();
}
/**
* Hexadecimal conversion string
* @param String str Byte string (no separator between Bytes Such as: [616C6B])
* @return String corresponding to 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);
}
/**
* Convert the string to a hexadecimal string
* @param String str The ASCII string to be converted
* @return String Each Byte is separated by a space, such as: [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();
}
private static String hexString = "0123456789ABCDEF";
public static String decode(String bytes)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
for (int i = 0; i < bytes.length(); i += 2)
baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
return new String(baos.toByteArray());
}
/**
* 将对应的字母转换ASCII值
*
* @param n
* @return
*/
private static String toHexUtil(int n)
{
String rt = "";
switch (n)
{
case 10:
rt += "A";
break;
case 11:
rt += "B";
break;
case 12:
rt += "C";
break;
case 13:
rt += "D";
break;
case 14:
rt += "E";
break;
case 15:
rt += "F";
break;
default:
rt += n;
}
return rt;
}
/**
* Convert letters to corresponding hexadecimal format
*
* @param n
* @return
*/
public static String toHex(int n)
{
StringBuilder sb = new StringBuilder( );
if (n / 16 == 0)
{
return toHexUtil(n);
}
else
{
String t = toHex(n / 16);
int nn = n % 16;
sb.append(t).append(toHexUtil(nn));
}
return sb.toString();
}
/** 将字符串解析为ASCII
*
* @param str
* @return
*/
public static String parseAscii(String str)
{
StringBuilder sb = new StringBuilder();
byte[] bs = str.getBytes();
for (int i = 0; i < bs.length; i++)
sb.append(toHex(bs[i]));
return sb.toString();
}