gpt4 book ai didi

java - Base64 解码为十六进制转储

转载 作者:行者123 更新时间:2023-12-02 10:05:48 25 4
gpt4 key购买 nike

我有一个像这样的 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

最佳答案

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 时不需要,但我更喜欢我的解决方案;)

关于java - Base64 解码为十六进制转储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55344244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com