把汉字转换成byte数组,其后再把byte数组转换成汉字
把汉字转换成byte数组,然后再把byte数组转换成汉字
/** * <把字符串转换成字节数组然后在封装成字符串> * <功能详细描述> * @param chinese * @return * @see [类、类#方法、类#成员] */ public static String chineseToString(String chinese) { if (Global.isEmpty(chinese)) { return ""; } else { // 定义StringBuffer StringBuffer sb = new StringBuffer(); // 把传进来的字符串转换成字节数组 byte[] b = chinese.getBytes(); byte[] temp = null; // 遍历字节数组,把字节数组转换成字符串 for (int i = 0; i < b.length; i++) { temp = new byte[4]; temp[0] = b[i]; temp[1] = 0; temp[2] = 0; temp[3] = 0; sb.append(lBytesToInt(temp)); if (i < b.length - 1) { sb.append("@"); } } return sb.toString(); } } /** * <把字节数组封装成的字符串转换成原来的字符串> * <功能详细描述> * @param stc * @return * @see [类、类#方法、类#成员] */ public static String stringToChinese(String stc) { // 如果传递的字符串为空则直接返回空 if (Global.isEmpty(stc)) { return ""; } else { // 分割字符串 String[] s = stc.split("@"); if (s.length > 0) { // 循环构造BYTE数组 byte[] b = new byte[s.length]; for (int i = 0; i < s.length; i++) { b[i] = (byte)Integer.parseInt(s[i]); } // 根据BYTE数组构造字符串 return new String(b); } else { return ""; } } } /** * 将低字节数组转换为int * @param b byte[] * @return int */ public static int lBytesToInt(byte[] b) { int s = 0; for (int i = 0; i < 3; i++) { if (b[3 - i] >= 0) { s = s + b[3 - i]; } else { s = s + 256 + b[3 - i]; } s = s * 256; } if (b[0] >= 0) { s = s + b[0]; } else { s = s + 256 + b[0]; } return s; }