Base64解码为十六进制转储

时间:2019-03-25 18:21:12

标签: java base64 hex

我有这样的base64字符串。

RALCgjQwMDA5wrNJb1QNAGNvYXBfbm9zZWNfMDEHZGVmYXVsdBEyIBRydD0yITLDncOiBElvVC9jb2FwX25vc2VjXzAxHQMxMjM0NS0wMDc3Nzg5Nzk5bShjb2FwOi8vMTkyLjE2OC4yNTAuMTE6MTU2ODUvSW9UL2NvYXBfbm9zZWNfMDEvZGVmYXVsdEEEw797Im0ybTpjaW4iOnsiY29uIjoiRXhhbXBsZUNJTiJ9fQ==

我想按如下所示将其转换为十六进制转储,

4402823430303039b3496f540d00636f61705f6e6f7365635f30310764656661756c741132201472743d322132dde204496f542f636f61705f6e6f7365635f30311d0331323334352d303037373738393739396d28636f61703a2f2f3139322e3136382e3235302e31313a31353638352f496f542f636f61705f6e6f7365635f30312f64656661756c744104ff7b226d326d3a63696e223a7b22636f6e223a224578616d706c6543494e227d7d

此转换已完成using this online tool

我正在尝试通过此Java代码实现相同的目标,

String base64 = "RALCgjQwMDA5wrNJb1QNAGNvYXBfbm9zZWNfMDEHZGVmYXVsdBEyIBRydD0yITLDncOiBElvVC9jb2FwX25vc2VjXzAxHQMxMjM0NS0wMDc3Nzg5Nzk5bShjb2FwOi8vMTkyLjE2OC4yNTAuMTE6MTU2ODUvSW9UL2NvYXBfbm9zZWNfMDEvZGVmYXVsdEEEw797Im0ybTpjaW4iOnsiY29uIjoiRXhhbXBsZUNJTiJ9fQ==";
byte[] bytes = Base64.getDecoder().decode(base64);
String hexDump = String.format("%x", new BigInteger(1, bytes));

但是结果不同。有什么线索代码有什么问题吗?

4402c2823430303039c2b3496f540d00636f61705f6e6f7365635f30310764656661756c741132201472743d322132c39dc3a204496f542f636f61705f6e6f7365635f30311d0331323334352d303037373738393739396d28636f61703a2f2f3139322e3136382e3235302e31313a31353638352f496f542f636f61705f6e6f7365635f30312f64656661756c744104c3bf7b226d326d3a63696e223a7b22636f6e223a224578616d706c6543494e227d7d

1 个答案:

答案 0 :(得分:0)

String base64 = "RALCgjQwMDA5wrNJb1QNAGNvYXBfbm9zZWNfMDEHZGVmYXVsdBEyIBRydD0yITLDncOiBElvVC9jb2FwX25vc2VjXzAxHQMxMjM0NS0wMDc3Nzg5Nzk5bShjb2FwOi8vMTkyLjE2OC4yNTAuMTE6MTU2ODUvSW9UL2NvYXBfbm9zZWNfMDEvZGVmYXVsdEEEw797Im0ybTpjaW4iOnsiY29uIjoiRXhhbXBsZUNJTiJ9fQ==";

byte[] bytes = Base64.getDecoder().decode(base64);

StringBuilder sbHexDump = new StringBuilder();
    for(byte b : bytes){
        sbHexDump.append(String.format("%02x", b));
    }
System.out.println(sbHexDump.toString());

格式说明符在这里很重要: “%02x”将产生4402c2(由于格式,第二个字节为2,它将产生02)。使用BigInteger时不需要,但我更喜欢我的解决方案;)