Base64解码为十六进制转储

问题描述 投票:0回答:1

我有这样的base64字符串。

RALCgjQwMDA5wrNJb1QNAGNvYXBfbm9zZWNfMDEHZGVmYXVsdBEyIBRydD0yITLDncOiBElvVC9jb2FwX25vc2VjXzAxHQMxMjM0NS0wMDc3Nzg5Nzk5bShjb2FwOi8vMTkyLjE2OC4yNTAuMTE6MTU2ODUvSW9UL2NvYXBfbm9zZWNfMDEvZGVmYXVsdEEEw797Im0ybTpjaW4iOnsiY29uIjoiRXhhbXBsZUNJTiJ9fQ==

我想把它转换成hex dump,如下所示,

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
java base64 hex
1个回答
1
投票
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时不需要,但我更喜欢我的解决方案;)

© www.soinside.com 2019 - 2024. All rights reserved.