如何将字节数组中的数据打印为字符? [英] How to print the data in byte array as characters?
问题描述
在我的字节数组中,我有一个消息的 hash 值,它由一些负值和正值组成.使用 (char)byte[i]
语句可以轻松打印正值.
In my byte array I have the hash values of a message which consists of some negative values and also positive values. Positive values are being printed easily by using the (char)byte[i]
statement.
现在我怎样才能得到负值?
Now how can I get the negative value?
推荐答案
好吧,如果您乐于以十进制形式打印它,您可以通过屏蔽使其成为正数:
Well if you're happy printing it in decimal, you could just make it positive by masking:
int positive = bytes[i] & 0xff;
不过,如果您要打印散列,则使用十六进制会更传统.关于 Stack Overflow 解决在 Java 中将二进制数据转换为十六进制字符串还有很多其他问题.
If you're printing out a hash though, it would be more conventional to use hex. There are plenty of other questions on Stack Overflow addressing converting binary data to a hex string in Java.
这篇关于如何将字节数组中的数据打印为字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!