如何使用System.out.println以十六进制打印字节? [英] How to print bytes in hexadecimal using System.out.println?

查看:162
本文介绍了如何使用System.out.println以十六进制打印字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明了一个字节数组(我正在使用Java):

I've declared a byte array (I'm using Java):

byte test[] = new byte[3];
test[0] = 0x0A;
test[1] = 0xFF;
test[2] = 0x01;

我如何打印存储在数组中的不同值?

How could I print the different values stored in the array?

如果我使用System.out.println(test [0]),它将打印'10'。我希望它能打印0x0A

If I use System.out.println(test[0]) it will print '10'. I'd like it to print 0x0A

感谢大家!

推荐答案

System.out.println(Integer.toHexString(test[0]));

(漂亮打印)

System.out.printf("0x%02X", test[0]);

(漂亮打印)

System.out.println(String.format("0x%02X", test[0]));

这篇关于如何使用System.out.println以十六进制打印字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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