I am converting a string into hexadecimal string and i want to convert that into octal string
I am doing it as follows
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")));
Output is
4920666f756e642074686520726561736f6e203220646179732061676f20692077617320746f6f20627573737920746f2075706461746520796f752e5468652070726f626c656d20776173207468617420746865205544484c20776173206d697373696e672e20492073686f756c6420686176652061646465642030362061742074686520626567696e6e696e672e2020697420616c6c20776f726b732066696e652e466f7220736f6d6520726561736f6e20492074686f75676874206b616e6e656c2061646473207468697320627920697473656c6620286e6f74207665727920646966666963756c742e2e2e292c20627574206e6f772049206b6e6f7720697420646f65736e27742e2e2e
I need to convert hex to octal string. I tried like this
String octal = Integer.toOctalString(Integer.parseInt(hex,16));
But as expected it gave me number format exception as hex string have some characters in it.
I want to know how can i conver hex string to octal string.
答案 0 :(得分:4)
As per the discussion in the comments, you want to convert each byte individually and convert them to octal:
String s = "My string to convert";
byte[] bytes = s.getBytes("UTF-8");
for (byte b : bytes) {
String octalValue = Integer.toString(b, 8);
// Do whatever
}
答案 1 :(得分:0)
The problem is, that your input (String hex
) is to big to be stored in a single integer.
3 hex digits correspond to 4 octal digits. So split your input string in chunks of three digits, use the conversion you already figured out, and concatenate the outputs.