如何将字节数组中的数据打印为字符 [英] how to print the data in byte array as characters

查看:110
本文介绍了如何将字节数组中的数据打印为字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的字节数组中,我有一个消息的哈希值,它由一些负值和正值组成。使用(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆