在 Java 中将十六进制字符串转换为 ASCII
我希望这不是一个太愚蠢的问题,我已经查看了 5 个不同的 Google 结果页面,但没有找到任何相关内容。
我需要做的是将包含所有十六进制字符的字符串转换为 ASCII 例如,
String fileName =
75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
我所看到的每种方式都使您看起来必须先将其放入数组中。有没有办法循环遍历每两个并转换它们?
I hope this isn't too much of a stupid question, I have looked on 5 different pages of Google results but haven't been able to find anything on this.
What I need to do is convert a string that contains all Hex characters into ASCII for example
String fileName =
75546f7272656e745c436f6d706c657465645c6e667375635f6f73745f62795f6d757374616e675c50656e64756c756d2d392c303030204d696c65732e6d7033006d7033006d7033004472756d202620426173730050656e64756c756d00496e2053696c69636f00496e2053696c69636f2a3b2a0050656e64756c756d0050656e64756c756d496e2053696c69636f303038004472756d2026204261737350656e64756c756d496e2053696c69636f30303800392c303030204d696c6573203c4d757374616e673e50656e64756c756d496e2053696c69636f3030380050656e64756c756d50656e64756c756d496e2053696c69636f303038004d50330000
Every way I have seen makes it seems like you have to put it into an array first. Is there no way to loop through each two and convert them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
只需使用 for 循环遍历字符串中的每对字符,将它们转换为一个字符,然后在字符串生成器的末尾敲击该字符:
或者(Java 8+)如果您感觉特别粗鲁,请使用臭名昭著的“固定宽度字符串分割”黑客使您能够使用流进行单行:
无论哪种方式,这都会给出以下几行:
嗯...:-)
Just use a for loop to go through each couple of characters in the string, convert them to a character and then whack the character on the end of a string builder:
Or (Java 8+) if you're feeling particularly uncouth, use the infamous "fixed width string split" hack to enable you to do a one-liner with streams instead:
Either way, this gives a few lines starting with the following:
Hmmm... :-)
使用 javax.xml.bind.DatatypeConverter 最简单的方法是:
Easiest way to do it with
javax.xml.bind.DatatypeConverter
:查看转换使用 Java 将十六进制转储到字节数组的字符串表示形式?
忽略编码等,您可以执行
new String (hexStringToByteArray("75546..."));
Check out Convert a string representation of a hex dump to a byte array using Java?
Disregarding encoding, etc. you can do
new String (hexStringToByteArray("75546..."));
据我了解,您需要取出连续的十六进制数字对,然后解码该两位十六进制数字并获取相应的字符:
So as I understand it, you need to pull out successive pairs of hex digits, then decode that 2-digit hex number and take the corresponding char:
对于这种情况,我有一个十六进制数据格式到一个 int 数组中,我想将它们转换为 String。
To this case, I have a hexadecimal data format into an int array and I want to convert them on String.