Last active
September 7, 2017 08:57
-
-
Save alanland/5874136 to your computer and use it in GitHub Desktop.
java 中 Hex的转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mobi.dzs.util; | |
/** | |
* 16进制值与String/Byte之间的转换 | |
* @author JerryLi | |
* @email [email protected] | |
* @data 2011-10-16 | |
* */ | |
public class CHexConver | |
{ | |
/** | |
* 字符串转换成十六进制字符串 | |
* @param String str 待转换的ASCII字符串 | |
* @return String 每个Byte之间空格分隔,如: [61 6C 6B] | |
*/ | |
public static String str2HexStr(String str) | |
{ | |
char[] chars = "0123456789ABCDEF".toCharArray(); | |
StringBuilder sb = new StringBuilder(""); | |
byte[] bs = str.getBytes(); | |
int bit; | |
for (int i = 0; i < bs.length; i++) | |
{ | |
bit = (bs[i] & 0x0f0) >> 4; | |
sb.append(chars[bit]); | |
bit = bs[i] & 0x0f; | |
sb.append(chars[bit]); | |
sb.append(' '); | |
} | |
return sb.toString().trim(); | |
} | |
/** | |
* 十六进制转换字符串 | |
* @param String str Byte字符串(Byte之间无分隔符 如:[616C6B]) | |
* @return String 对应的字符串 | |
*/ | |
public static String hexStr2Str(String hexStr) | |
{ | |
String str = "0123456789ABCDEF"; | |
char[] hexs = hexStr.toCharArray(); | |
byte[] bytes = new byte[hexStr.length() / 2]; | |
int n; | |
for (int i = 0; i < bytes.length; i++) | |
{ | |
n = str.indexOf(hexs[2 * i]) * 16; | |
n += str.indexOf(hexs[2 * i + 1]); | |
bytes[i] = (byte) (n & 0xff); | |
} | |
return new String(bytes); | |
} | |
/** | |
* bytes转换成十六进制字符串 | |
* @param byte[] b byte数组 | |
* @return String 每个Byte值之间空格分隔 | |
*/ | |
public static String byte2HexStr(byte[] b) | |
{ | |
String stmp=""; | |
StringBuilder sb = new StringBuilder(""); | |
for (int n=0;n<b.length;n++) | |
{ | |
stmp = Integer.toHexString(b[n] & 0xFF); | |
sb.append((stmp.length()==1)? "0"+stmp : stmp); | |
sb.append(" "); | |
} | |
return sb.toString().toUpperCase().trim(); | |
} | |
/** | |
* bytes字符串转换为Byte值 | |
* @param String src Byte字符串,每个Byte之间没有分隔符 | |
* @return byte[] | |
*/ | |
public static byte[] hexStr2Bytes(String src) | |
{ | |
int m=0,n=0; | |
int l=src.length()/2; | |
System.out.println(l); | |
byte[] ret = new byte[l]; | |
for (int i = 0; i < l; i++) | |
{ | |
m=i*2+1; | |
n=m+1; | |
ret[i] = Byte.decode("0x" + src.substring(i*2, m) + src.substring(m,n)); | |
} | |
return ret; | |
} | |
/** | |
* String的字符串转换成unicode的String | |
* @param String strText 全角字符串 | |
* @return String 每个unicode之间无分隔符 | |
* @throws Exception | |
*/ | |
public static String strToUnicode(String strText) | |
throws Exception | |
{ | |
char c; | |
StringBuilder str = new StringBuilder(); | |
int intAsc; | |
String strHex; | |
for (int i = 0; i < strText.length(); i++) | |
{ | |
c = strText.charAt(i); | |
intAsc = (int) c; | |
strHex = Integer.toHexString(intAsc); | |
if (intAsc > 128) | |
str.append("\\u" + strHex); | |
else // 低位在前面补00 | |
str.append("\\u00" + strHex); | |
} | |
return str.toString(); | |
} | |
/** | |
* unicode的String转换成String的字符串 | |
* @param String hex 16进制值字符串 (一个unicode为2byte) | |
* @return String 全角字符串 | |
*/ | |
public static String unicodeToString(String hex) | |
{ | |
int t = hex.length() / 6; | |
StringBuilder str = new StringBuilder(); | |
for (int i = 0; i < t; i++) | |
{ | |
String s = hex.substring(i * 6, (i + 1) * 6); | |
// 高位需要补上00再转 | |
String s1 = s.substring(2, 4) + "00"; | |
// 低位直接转 | |
String s2 = s.substring(4); | |
// 将16进制的string转为int | |
int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16); | |
// 将int转换为字符 | |
char[] chars = Character.toChars(n); | |
str.append(new String(chars)); | |
} | |
return str.toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
String hexStr = "0123456789ABCDEF"; | |
long i = Long.valueOf(hexStr, 16); | |
System.out.println(Long.toHexString(i)); | |
/// | |
public static String toHexString(byte[] ba) { | |
StringBuilder str = new StringBuilder(); | |
for(int i = 0; i < ba.length; i++) | |
str.append(String.format("%x", ba[i])); | |
return str.toString(); | |
} | |
public static String fromHexString(String hex) { | |
StringBuilder str = new StringBuilder(); | |
for (int i = 0; i < hex.length(); i+=2) { | |
str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16)); | |
} | |
return str.toString(); | |
} | |
/// | |
byte[] bytes = string.getBytes(CHARSET); // you didn't say what charset you wanted | |
BigInteger bigInt = new BigInteger(bytes); | |
String hexString = bigInt.toString(16); // 16 is the radix | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.codec.DecoderException; | |
import org.apache.commons.codec.binary.Hex; | |
import java.math.BigInteger; | |
import java.util.Arrays; | |
/** | |
* Date: 2013-06-26 3:45 PM | |
* | |
* @author WangChengyi | |
*/ | |
public class HexStr { | |
public static String toHex(String arg) { | |
return String.format("%040x", new BigInteger(arg.getBytes(/*YOUR_CHARSET?*/))); | |
return String.format("%x", new BigInteger(arg.getBytes(/*YOUR_CHARSET?*/))); | |
} | |
public static void main2(String[] args) throws DecoderException { | |
System.out.println(toHex("mat12345")); | |
System.out.println(Hex.encodeHex("mat12345".getBytes())); | |
System.out.println(new String(Hex.decodeHex("00006d61743132333435".toCharArray()))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Date: 2013-06-26 3:56 PM | |
* | |
* @author WangChengyi | |
*/ | |
public class StringToHex { | |
public String convertStringToHex(String str){ | |
char[] chars = str.toCharArray(); | |
StringBuffer hex = new StringBuffer(); | |
for(int i = 0; i < chars.length; i++){ | |
hex.append(Integer.toHexString((int)chars[i])); | |
} | |
return hex.toString(); | |
} | |
public 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); | |
} | |
System.out.println("Decimal : " + temp.toString()); | |
return sb.toString(); | |
} | |
public static void main(String[] args) { | |
StringToHex strToHex = new StringToHex(); | |
System.out.println("\n***** Convert ASCII to Hex *****"); | |
String str = "I Love Java!"; | |
System.out.println("Original input : " + str); | |
String hex = strToHex.convertStringToHex(str); | |
System.out.println("Hex : " + hex); | |
System.out.println("\n***** Convert Hex to ASCII *****"); | |
System.out.println("Hex : " + hex); | |
System.out.println("ASCII : " + strToHex.convertHexToString(hex)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
About CHexConver.java method hexStr2Str on the line 40 _maybe there are some bug.
For example,
Byte.decode("0xcc")
will not be executed,although 0xcc isn't a acsii value.Here are my code:
public static byte[] HexStringToBinary(String src){ int m=0,n=0; int len=src.length()/2; System.out.println(len); byte[] ret = new byte[len]; for (int i = 0; i < len; i++) { m=i*2+1; n=m+1; //Height nibble int h = Byte.decode("0x" + src.substring(i*2, m))<<4; //Low nibble int l=Byte.decode("0x"+ src.substring(m,n)); ret[i]=(byte)(h|l); } return ret; }