简体   繁体   English

JAVA转换十六进制字符串

[英]JAVA Converting Hexadecimal String

In Python, I can convert hex to something like this.. 在Python中,我可以将十六进制转换成这样的形式。

s = "6025ce2069c61b15d8314f7cdd76b850dcd339547335d0e4a1e0b9915b0230cd"

s.decode('hex')
'`%\xce i\xc6\x1b\x15\xd81O|\xddv\xb8P\xdc\xd39Ts5\xd0\xe4\xa1\xe0\xb9\x91[\x020\xcd'

My question is how to do the same thing in Java? 我的问题是如何在Java中做同样的事情?

I did this in Java like this but it raises an exception. 我是这样用Java完成的,但它引发了一个异常。

new String(Hex.decodeHex(str.toCharArray()), "UTF-8"

The error messege is like this. 错误消息是这样的。

Exception in thread "main" org.apache.commons.codec.DecoderException: Odd number of characters.

======================================================== ================================================== ======

I removed UTF-8 but I am still getting the same exception.. please help! 我删除了UTF-8,但仍然遇到相同的异常..请帮助!

new String(Hex.decodeHex(combined.toCharArray()))

Exception in thread "main" org.apache.commons.codec.DecoderException: Odd number of characters.

This works for me 这对我有用

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"));
    }

Output 输出量

`%? i??1O|?v?P??9Ts5???[0?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM