/**
*******************************************************************************
* Simple To Introduction
*
* 项 目 名:CC
* 包 路 径:com.z.c.Util.java
* 模 块 名:(手动填写)
* 创 建 人:
* 创建时间:2015-12-11 下午2:50:56
* 修改历史:
* 日期修改人描述
* -----------------------------------------------------------------------------
*
* 版 本:1.0
* Copyright © 2012 . All Rights Reserved.
*******************************************************************************
*/
package com.z.c;
public class Util {
/**
* 将16进制字符串数转换为汉字,可包含数字及符号
* 方 法 名:HexToUnicode
* 创 建 人:
* 创建时间:2015-12-11 下午2:58:48
* 修 改 人:
* 修改日期:
* @param content
* @return String
*/
public static String HexToUnicode(String content) {
String enUnicode = null;
String deUnicode = null;
for (int i = 0; i < content.length(); i++) {
if (enUnicode == null) {
enUnicode = String.valueOf(content.charAt(i));
} else {
enUnicode = enUnicode + content.charAt(i);
}
if (i % 4 == 3) {
if (enUnicode != null) {
if (deUnicode == null) {
deUnicode = String.valueOf((char) Integer.valueOf(enUnicode, 16).intValue());
} else {
deUnicode = deUnicode + String.valueOf((char) Integer.valueOf(enUnicode, 16).intValue());
}
}
enUnicode = null;
}
}
return deUnicode;
}
/**
* 将汉字转换为16进制字符串数,可包含数字及符号
* 方 法 名:UnicodeToHex
* 创 建 人:
* 创建时间:2015-12-11 下午2:59:31
* 修 改 人:
* 修改日期:
* @param content
* @return String
*/
public static String UnicodeToHex(String content) {
String enUnicode = null;
for (int i = 0; i < content.length(); i++) {
if (i == 0) {
enUnicode = getHexString(Integer.toHexString(content.charAt(i)).toUpperCase());
} else {
enUnicode = enUnicode + getHexString(Integer.toHexString(content.charAt(i)).toUpperCase());
}
}
return enUnicode;
}
private static String getHexString(String hexString) {
String hexStr = "";
for (int i = hexString.length(); i < 4; i++) {
if (i == hexString.length())
hexStr = "0";
else
hexStr = hexStr + "0";
}
return hexStr + hexString;
}
public static void main(String[] args) {
String str = "王江波1234是一个小朋友,HOHOHOHO";
System.out.println(Util.UnicodeToHex("王江波"));
// 打印出
// 738B6C5F6CE2662F4E004E2A5C0F670B53CBFF0C0048004F0048004F0048004F0048004F
str = "738B6C5F6CE20031003200330034662F4E004E2A5C0F670B53CBFF0C0048004F0048004F0048004F0048004F";
System.out.println(Util.HexToUnicode(str));
// 打印出 王江波是一个小朋友,HOHOHOHO
}
}