gpt4 book ai didi

java - java中如何将十六进制字符串转换为八进制字符串

转载 作者:行者123 更新时间:2023-11-29 03:08:01 25 4
gpt4 key购买 nike

我正在将一个字符串转换为十六进制字符串,我想将其转换为八进制字符串

我是这样做的

String s="I found the reason 2 days ago i was too bussy to update you.The problem was that the UDHL was missing. I should have added 06 at the beginning.  it all works fine.For some reason I thought kannel adds this by itself (not very difficult...), but now I know it doesn't...";


String hex = String.format("%040x", new BigInteger(1, s.getBytes("UTF-8")));

输出是

4920666f756e642074686520726561736f6e203220646179732061676f20692077617320746f6f20627573737920746f2075706461746520796f752e5468652070726f626c656d20776173207468617420746865205544484c20776173206d697373696e672e20492073686f756c6420686176652061646465642030362061742074686520626567696e6e696e672e2020697420616c6c20776f726b732066696e652e466f7220736f6d6520726561736f6e20492074686f75676874206b616e6e656c2061646473207468697320627920697473656c6620286e6f74207665727920646966666963756c742e2e2e292c20627574206e6f772049206b6e6f7720697420646f65736e27742e2e2e

我需要将十六进制字符串转换为八进制字符串。我试过这样

String octal = Integer.toOctalString(Integer.parseInt(hex,16));

但正如预期的那样,它给了我数字格式异常,因为十六进制字符串中有一些字符。

我想知道如何将十六进制字符串转换为八进制字符串。

最佳答案

根据评论中的讨论,您想单独转换每个字节并将它们转换为八进制:

String s     = "My string to convert";
byte[] bytes = s.getBytes("UTF-8");

for (byte b : bytes) {
String octalValue = Integer.toString(b, 8);

// Do whatever
}

关于java - java中如何将十六进制字符串转换为八进制字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069857/

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