JAVA 转换十六进制字符串
JAVA Converting Hexadecimal String
在 Python 中,我可以将十六进制转换为这样的东西..
s = "6025ce2069c61b15d8314f7cdd76b850dcd339547335d0e4a1e0b9915b0230cd"
s.decode('hex')
'`%\xce i\xc6\x1b\x15\xd81O|\xddv\xb8P\xdc\xd39Ts5\xd0\xe4\xa1\xe0\xb9\x91[\x020\xcd'
我的问题是如何在 Java 中做同样的事情?
我在 Java 中这样做是这样的,但它引发了异常。
new String(Hex.decodeHex(str.toCharArray()), "UTF-8"
报错信息是这样的。
Exception in thread "main" org.apache.commons.codec.DecoderException: Odd number of characters.
=========================================== ===========
我删除了 UTF-8,但我仍然遇到同样的异常。请帮忙!
new String(Hex.decodeHex(combined.toCharArray()))
Exception in thread "main" org.apache.commons.codec.DecoderException: Odd number of characters.
这对我有用
public static void main(String[] args) throws ParseException, DecoderException, UnsupportedEncodingException {
String hexString = "6025ce2069c61b15d8314f7cdd76b850dcd339547335d0e4a1e0b9915b0230cd";
byte[] bytes = Hex.decodeHex(hexString.toCharArray());
System.out.println(new String(bytes , "UTF-8"));
}
输出
`%? i??1O|?v?P??9Ts5???[0?
在 Python 中,我可以将十六进制转换为这样的东西..
s = "6025ce2069c61b15d8314f7cdd76b850dcd339547335d0e4a1e0b9915b0230cd"
s.decode('hex')
'`%\xce i\xc6\x1b\x15\xd81O|\xddv\xb8P\xdc\xd39Ts5\xd0\xe4\xa1\xe0\xb9\x91[\x020\xcd'
我的问题是如何在 Java 中做同样的事情?
我在 Java 中这样做是这样的,但它引发了异常。
new String(Hex.decodeHex(str.toCharArray()), "UTF-8"
报错信息是这样的。
Exception in thread "main" org.apache.commons.codec.DecoderException: Odd number of characters.
=========================================== ===========
我删除了 UTF-8,但我仍然遇到同样的异常。请帮忙!
new String(Hex.decodeHex(combined.toCharArray()))
Exception in thread "main" org.apache.commons.codec.DecoderException: Odd number of characters.
这对我有用
public static void main(String[] args) throws ParseException, DecoderException, UnsupportedEncodingException {
String hexString = "6025ce2069c61b15d8314f7cdd76b850dcd339547335d0e4a1e0b9915b0230cd";
byte[] bytes = Hex.decodeHex(hexString.toCharArray());
System.out.println(new String(bytes , "UTF-8"));
}
输出
`%? i??1O|?v?P??9Ts5???[0?