| | |
| | | package com.boying.util; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.UUID; |
| | | |
| | | public class StringUtil { |
| | | |
| | | /** 16进制中的字符集 */ |
| | | private static final String HEX_CHAR = "0123456789ABCDEF"; |
| | | |
| | | /** 16进制中的字符集对应的字节数组 */ |
| | | private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes(); |
| | | |
| | | public static boolean isNullOrEmpty(String str) { |
| | | if (null == str || "".equalsIgnoreCase(str.trim()) |
| | |
| | | } |
| | | return false; |
| | | } |
| | | public static byte[] convertNum(String s){ |
| | | String[] split = s.split("-"); |
| | | byte[] bytes = new byte[split.length]; |
| | | for (int i = 0; i < split.length; i++) { |
| | | String s1 = split[i]; |
| | | byte b = Byte.parseByte("0x"+s1); |
| | | bytes[i]=b; |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | public static String toStr(String[] strs){ |
| | | String s = Arrays.toString(strs); |
| | | String substring = s.substring(1, s.length() - 1); |
| | | String s1 = substring.replaceAll(",", ""); |
| | | String s2 = s1.replaceAll(" ", ""); |
| | | return s2; |
| | | } |
| | | |
| | | public static byte[] toBytes(String strs){ |
| | | String[] split = strs.split("-"); |
| | | byte[] bytes = new byte[split.length]; |
| | | for (int i = 0; i < split.length; i++) { |
| | | String s = split[i]; |
| | | bytes[i] = (byte) Byte.parseByte(s); |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | public static byte[] hex2Byte(String hex) { |
| | | System.out.println(); |
| | | String[] parts = hex.split("-"); |
| | | byte[] bytes = new byte[parts.length]; |
| | | for (int i = 0; i < parts.length; i++) { |
| | | bytes[i] = (byte) (Integer.parseInt(parts[i],10)); |
| | | System.out.print(bytes[i]+ " "); |
| | | } |
| | | return bytes; |
| | | } |
| | | |
| | | public static String getUUID() { |
| | | return UUID.randomUUID().toString().replaceAll("-", ""); |
| | | } |
| | | |
| | | /** |
| | | * 将指定byte数组以16进制的形式打印到控制台 |
| | | * |
| | | * @param hint String |
| | | * @param b byte[] |
| | | * @return void |
| | | */ |
| | | public static void printHexString(String hint, byte[] b) { |
| | | System.out.print(hint); |
| | | for (int i = 0; i < b.length; i++) { |
| | | String hex = Integer.toHexString(b[i] & 0xFF); |
| | | if (hex.length() == 1) { |
| | | hex = '0' + hex; |
| | | } |
| | | System.out.print(hex.toUpperCase() + " "); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @param b byte[] |
| | | * @return String |
| | | */ |
| | | public static String Bytes2HexString(byte[] b) { |
| | | String ret = ""; |
| | | for (int i = 0; i < b.length; i++) { |
| | | String hex = Integer.toHexString(b[i] & 0xFF); |
| | | if (hex.length() == 1) { |
| | | hex = '0' + hex; |
| | | } |
| | | ret += " 0x" + hex.toUpperCase(); |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | /** |
| | | * 将指定字符串src,以每两个字符分割转换为16进制形式 如:"2B44EFD9" –> byte[]{0x2B, 0×44, 0xEF, |
| | | * 0xD9} |
| | | * |
| | | * @param src String |
| | | * @return byte[] |
| | | */ |
| | | public static byte[] HexString2Bytes(String src) { |
| | | if (null == src || 0 == src.length()) { |
| | | return null; |
| | | } |
| | | // String s1 = src.replaceAll("-", ""); |
| | | byte[] ret = new byte[src.length() / 2]; |
| | | byte[] tmp = src.getBytes(); |
| | | for (int i = 0; i < (tmp.length / 2); i++) { |
| | | ret[i] = (byte)uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | /** |
| | | * 将两个ASCII字符合成一个字节; 如:"EF"–> 0xEF |
| | | * |
| | | * @param src0 byte |
| | | * @param src1 byte |
| | | * @return byte |
| | | */ |
| | | public static byte uniteBytes(byte src0, byte src1) { |
| | | byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); |
| | | _b0 = (byte) (_b0 << 4); |
| | | byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); |
| | | byte ret = (byte) (_b0 ^ _b1); |
| | | return ret; |
| | | } |
| | | |
| | | /** |
| | | * 10进制字节数组转换为16进制字节数组 |
| | | * |
| | | * byte用二进制表示占用8位,16进制的每个字符需要用4位二进制位来表示,则可以把每个byte |
| | | * 转换成两个相应的16进制字符,即把byte的高4位和低4位分别转换成相应的16进制字符,再取对应16进制字符的字节 |
| | | * |
| | | * @param b 10进制字节数组 |
| | | * @return 16进制字节数组 |
| | | */ |
| | | public static byte[] byte2hex(byte[] b) { |
| | | int length = b.length; |
| | | byte[] b2 = new byte[length << 1]; |
| | | int pos; |
| | | for(int i=0; i<length; i++) { |
| | | pos = 2*i; |
| | | b2[pos] = HEX_STRING_BYTE[(b[i] & 0xf0) >> 4]; |
| | | b2[pos+1] = HEX_STRING_BYTE[b[i] & 0x0f]; |
| | | } |
| | | return b2; |
| | | } |
| | | |
| | | /** |
| | | * 16进制字节数组转换为10进制字节数组 |
| | | * |
| | | * 两个16进制字节对应一个10进制字节,则将第一个16进制字节对应成16进制字符表中的位置(0~15)并向左移动4位, |
| | | * 再与第二个16进制字节对应成16进制字符表中的位置(0~15)进行或运算,则得到对应的10进制字节 |
| | | * @param b 10进制字节数组 |
| | | * @return 16进制字节数组 |
| | | */ |
| | | public static byte[] hex2byte(byte[] b) { |
| | | if(b.length%2 != 0) { |
| | | throw new IllegalArgumentException("byte array length is not even!"); |
| | | } |
| | | |
| | | int length = b.length >> 1; |
| | | byte[] b2 = new byte[length]; |
| | | int pos; |
| | | for(int i=0; i<length; i++) { |
| | | pos = i << 1; |
| | | b2[i] = (byte) (HEX_CHAR.indexOf( b[pos] ) << 4 | HEX_CHAR.indexOf( b[pos+1] ) ); |
| | | } |
| | | return b2; |
| | | } |
| | | |
| | | /** |
| | | * 将16进制字节数组转成10进制字符串 |
| | | * @param b 16进制字节数组 |
| | | * @return 10进制字符串 |
| | | */ |
| | | public static String hex2Str(byte[] b) { |
| | | return new String(hex2byte(b)); |
| | | } |
| | | |
| | | /** |
| | | * 将10进制字节数组转成16进制字符串 |
| | | * @param b 10进制字节数组 |
| | | * @return 16进制字符串 |
| | | */ |
| | | public static String byte2HexStr(byte[] b) { |
| | | return Integer.toHexString(Integer.parseInt(new String(b))); |
| | | } |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | |
| | | } |
| | | } |