如何将字节数组中的数据打印为字符? [英] 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.
现在如何获得负值?
推荐答案
如果您乐于以十进制格式打印,则可以通过屏蔽使其为正值:
Well if you're happy printing it in decimal, you could just make it positive by masking:
int positive = bytes[i] & 0xff;
但是,如果要打印散列,则使用十六进制会更常规.关于堆栈溢出,还有很多其他问题要解决,即在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屋!
查看全文