如何将不可打印的字符或字符串转换为十六进制?
我有包含下一个十六进制演示文稿的字符串: “5f e8 d0 7b c0 f7 54 07 fb e4 20 f5 b8 10 67 a9”
您知道这只是十六进制,我需要从字符串中获取此十六进制表示。字符串看起来像: "ED>@@2.WW'KJ%z_{T g"
那么,如何从 "ED>@@2.WW'KJ%z_{T g" 十六进制表示 "5f e8 d0 7b c0 f7 54 07 fb e4 20 f5 b8 10 67 a9"?这是不可打印的字符,所以我不能使用它: <代码>
public static String stringToHex(String arg) {
return String.format("%x", new BigInteger(arg.getBytes()));
}
result: -10404282104042104042104042104042104042c7eea21040428189104042104042f5. And also this returns me something strange: System.out.println(String.format("%h", Integer.toHexString(buff.charAt(0))));
result: 6d1.这段代码有时会起作用。数据来自套接字(作为字符串,因为我需要获得许多作为字符串的答案,并且只有这个身份验证挑战作为十六进制)。
I have String which contain the next hex presentation:
"5f e8 d0 7b c0 f7 54 07 fb e4 20 f5 b8 10 67 a9"
You understand that this is just hex and I need to get this hex presentation from String. String looks like:
"ED>@@2.W.W'KJ%z_{T g"
So, how to get from "ED>@@2.W.W'KJ%z_{T g" hex presentation "5f e8 d0 7b c0 f7 54 07 fb e4 20 f5 b8 10 67 a9"? This is unprintable characters so I can't use this:
public static String stringToHex(String arg) { return String.format("%x", new BigInteger(arg.getBytes())); }
result: -10404282104042104042104042104042104042c7eea21040428189104042104042f5.
And also this returns me something strange:
System.out.println(String.format("%h", Integer.toHexString(buff.charAt(0))));
result: 6d1.
And this code sometimes works. The data comes from socket (as String because I need to get many answers as String and only this Auth Challenge as hex).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是正确的解决方案:
使用
Integer.toHexString()
的解决方案是错误的,原因如下:This is the correct solution:
Solution with
Integer.toHexString()
is wrong for the following reasons: