public class HexUtil
{
/**
* 16進制中的字符集
*/
private static final String HEX_CHAR = "0123456789ABCDEF";
/**
* 16進制中的字符集對應(yīng)的字節(jié)數(shù)組
*/
private static final byte[] HEX_STRING_BYTE = HEX_CHAR.getBytes();
/**
* 10進制字節(jié)數(shù)組轉(zhuǎn)換為16進制字節(jié)數(shù)組
* <p>
* byte用二進制表示占用8位聪全,16進制的每個字符需要用4位二進制位來表示,則可以把每個byte
* 轉(zhuǎn)換成兩個相應(yīng)的16進制字符,即把byte的高4位和低4位分別轉(zhuǎn)換成相應(yīng)的16進制字符葫隙,再取對應(yīng)16進制字符的字節(jié)
*
* @param b 10進制字節(jié)數(shù)組
* @return 16進制字節(jié)數(shù)組
*/
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進制字節(jié)數(shù)組轉(zhuǎn)換為10進制字節(jié)數(shù)組
* <p>
* 兩個16進制字節(jié)對應(yīng)一個10進制字節(jié),則將第一個16進制字節(jié)對應(yīng)成16進制字符表中的位置(0~15)并向左移動4位,
* 再與第二個16進制字節(jié)對應(yīng)成16進制字符表中的位置(0~15)進行或運算麦撵,則得到對應(yīng)的10進制字節(jié)
*
* @param b 10進制字節(jié)數(shù)組
* @return 16進制字節(jié)數(shù)組
*/
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進制字節(jié)數(shù)組轉(zhuǎn)成10進制字符串
*
* @param b 16進制字節(jié)數(shù)組
* @return 10進制字符串
*/
public static String hex2Str(byte[] b)
{
return new String(hex2byte(b));
}
/**
* 將10進制字節(jié)數(shù)組轉(zhuǎn)成16進制字符串
*
* @param b 10進制字節(jié)數(shù)組
* @return 16進制字符串
*/
public static String byte2HexStr(byte[] b)
{
return Integer.toHexString(Integer.parseInt(new String(b)));
}
public static byte[] hexString2Bytes(String hex)
{
if ((hex == null) || (hex.equals(""))) {
return null;
} else if (hex.length() % 2 != 0) {
return null;
} else {
hex = hex.toUpperCase();
int len = hex.length() / 2;
byte[] b = new byte[len];
char[] hc = hex.toCharArray();
for (int i = 0; i < len; i++) {
int p = 2 * i;
b[i] = (byte) (charToByte(hc[p]) << 4 | charToByte(hc[p + 1]));
}
return b;
}
}
/*
* 字符轉(zhuǎn)換為字節(jié)
*/
private static byte charToByte(char c)
{
return (byte) "0123456789ABCDEF".indexOf(c);
}
/**
* 字節(jié)數(shù)組轉(zhuǎn)為普通字符串(ASCII對應(yīng)的字符)
*
* @param bytearray byte[]
* @return String
*/
public static String bytetoString(byte[] bytearray)
{
String result = "";
char temp;
int length = bytearray.length;
for (int i = 0; i < length; i++) {
temp = (char) bytearray[i];
result += temp;
}
return result;
}
/**
* 數(shù)字字符串轉(zhuǎn)ASCII碼字符串
*
* @param content 字符串
* @return ASCII字符串
*/
public static String StringToAsciiString(String content)
{
String result = "";
int max = content.length();
for (int i = 0; i < max; i++) {
char c = content.charAt(i);
String b = Integer.toHexString(c);
result = result + b;
}
return result;
}
/**
* 十六進制字符串裝十進制
*
* @param hex 十六進制字符串
* @return 十進制數(shù)值
*/
public static int hexStringToAlgorism(String hex)
{
hex = hex.toUpperCase();
int max = hex.length();
int result = 0;
for (int i = max; i > 0; i--) {
char c = hex.charAt(i - 1);
int algorism = 0;
if (c >= '0' && c <= '9') {
algorism = c - '0';
} else {
algorism = c - 55;
}
result += Math.pow(16, max - i) * algorism;
}
return result;
}
/**
* ASCII碼字符串轉(zhuǎn)數(shù)字字符串
*
* @param content ASCII字符串
* @return 字符串
*/
public static String AsciiStringToString(String content)
{
String result = "";
int length = content.length() / 2;
for (int i = 0; i < length; i++) {
String c = content.substring(i * 2, i * 2 + 2);
int a = hexStringToAlgorism(c);
char b = (char) a;
String d = String.valueOf(b);
result += d;
}
return result;
}
/***
* Ascii轉(zhuǎn)16進制
* @param str
* @return
*/
public static String convertStringToHex(String str)
{
char[] chars = str.toCharArray();
StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
hex.append(Integer.toHexString((byte) chars[i]));
}
return hex.toString();
}
/***
* 16進制轉(zhuǎn)ASCII
* @param hex
* @return
*/
public static String convertHexToString(String hex)
{
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
//49204c6f7665204a617661 split into two characters 49, 20, 4c...
for (int i = 0; i < hex.length() - 1; i += 2) {
//grab the hex in pairs
String output = hex.substring(i, (i + 2));
//convert hex to decimal
int decimal = Integer.parseInt(output, 16);
//convert the decimal to character
sb.append((char) decimal);
temp.append(decimal);
}
return sb.toString();
}
/***
* byte[] z轉(zhuǎn)int
* @param b
* @return
*/
public static int bytes2int(byte[] b)
{
int mask = 0xff;
int temp = 0;
int res = 0;
for (int i = 0; i < 4; i++) {
res <<= 8;
temp = b[i] & mask;
res |= temp;
}
return res;
}
public static byte[] int2bytes(int num)
{
byte[] b = new byte[4];
for (int i = 0; i < 4; i++) {
b[i] = (byte) (num >>> (24 - i * 8));
}
return b;
}
/**
* 中文轉(zhuǎn)Unicode
*
* @param gbString
* @return
*/
public static String UnicodeEncoding(String gbString)
{ //gbString = "測試"
char[] utfBytes = gbString.toCharArray(); //utfBytes = [測, 試]
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
String hexB = Integer.toHexString(utfBytes[byteIndex]); //轉(zhuǎn)換為16進制整型字符串
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
return unicodeBytes;
}
/**
* Unicode轉(zhuǎn)中文
*/
public static String decodeUnicode(String dataStr)
{
int start = 0;
int end = 0;
StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("\\u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16進制parse整形字符串上遥。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
/**
* gb2312編碼
*/
public static String gb2312decode(String string) throws UnsupportedEncodingException
{
byte[] bytes = new byte[string.length() / 2];
for (int i = 0; i < bytes.length; i++) {
byte high = Byte.parseByte(string.substring(i * 2, i * 2 + 1), 16);
byte low = Byte.parseByte(string.substring(i * 2 + 1, i * 2 + 2), 16);
bytes[i] = (byte) (high << 4 | low);
}
return new String(bytes, "gb2312");
}
/**
* gb2312解碼
*/
public static String gb2312eecode(String string) throws UnsupportedEncodingException
{
StringBuffer gbkStr = new StringBuffer();
byte[] gbkDecode = string.getBytes("gb2312");
for (byte b : gbkDecode) {
gbkStr.append(Integer.toHexString(b & 0xFF));
}
return gbkStr.toString();
}
/**
* 和校驗
* SUM(cmd, Length, Data0…DataN)^0xFF
* */
public static byte getCheckSum(byte[] packBytes){
int checkSum = 0;
for (int i = 0; i < packBytes.length; i++) {
checkSum += packBytes[i];
}
checkSum &= 0xff;
return (byte)checkSum;
}
}
進制轉(zhuǎn)換的工具類
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門携狭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人回俐,你說我怎么就攤上這事逛腿。” “怎么了仅颇?”我有些...
- 文/不壞的土叔 我叫張陵单默,是天一觀的道長。 經(jīng)常有香客問我忘瓦,道長搁廓,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任政冻,我火速辦了婚禮枚抵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘明场。我一直安慰自己汽摹,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布苦锨。 她就那樣靜靜地躺著逼泣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪舟舒。 梳的紋絲不亂的頭發(fā)上拉庶,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼慷蠕!你這毒婦竟也來了珊拼?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布旦万,位于F島的核電站,受9級特大地震影響镶蹋,放射性物質(zhì)發(fā)生泄漏成艘。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒙蒙 一贺归、第九天 我趴在偏房一處隱蔽的房頂上張望淆两。 院中可真熱鬧,春花似錦拂酣、人聲如沸秋冰。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽剑勾。三九已至,卻和暖如春赵颅,著一層夾襖步出監(jiān)牢的瞬間虽另,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 為了讓基本數(shù)據(jù)類型也具備對象的特性晒喷, Java 為每個基本數(shù)據(jù)類型都提供了一個包裝類孝偎,這樣我們就可以像操作對象那樣...
- 我初中上了半年,就下學(xué)了凉敲,在我們市區(qū)上了幾個月班衣盾,磨玉,年底回家了爷抓,第二年去了廈門势决,好城市呀,在一個做頭盔的廠上班...
- 從來沒有這么震驚于一件事的發(fā)現(xiàn)帶給自己的震撼蓝撇,那就是以時間為基礎(chǔ)的復(fù)利效應(yīng)果复。今年五月初,偶然間讀到水湄姐的...