package com.boying.util; import java.util.Arrays; import java.util.UUID; public class StringUtil { public static boolean isNullOrEmpty(String str) { if (null == str || "".equalsIgnoreCase(str.trim()) || "null".equals(str.trim())) { return true; } 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 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[s1.length() / 2]; byte[] tmp = s1.getBytes(); for (int i = 0; i < (tmp.length / 2); i++) { ret[i] = 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; } public static void main(String[] args) { String s = "00-64-FF-FF-6E-78-00-04-00-01-01-02-00-FF-00-00-00-09-E4-BA-AC-41-38-38-38-38-38-0D-01-01-01-02-00-FF-00-00-00-09-E4-B8-B4-E6-97-B6-E8-BD-A6-0D-02-01-01-02-00-FF-00-00-00-0C-E6-AC-A2-E8-BF-8E-E5-85-89-E4-B8-B4-0D-03-01-01-02-00-FF-00-00-00-09-E4-BD-99-E4-BD-8D-3A-37-34-00-0A-20-E4-B8-B4-E6-97-B6-E8-BD-A6-2C-E4-BA-AC-41-38-38-38-38-38-2C-E6-AC-A2-E8-BF-8E-E5-85-89-E4-B8-B4-00-6B-E7"; String s1 = s.replaceAll("-", ""); byte[] bytes = HexString2Bytes(s1); for (byte aByte : bytes) { System.out.print(aByte + " "); } } }