String.getBytes() 结果不一致
System.out.println("hello world".getBytes("UTF-8"));
有时返回不同的值,这是为什么?
抱歉,我还是 Java 菜鸟。
System.out.println("hello world".getBytes("UTF-8"));
occasionally returns a different value, why is that??
Sorry, I'm still a noob at Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这段代码打印一个数组(byte[]),但是Java中没有标准的数组打印。因此,代码不是打印数组的内容,而是显示对数组的一些神秘的内存引用。例如“[B@6bbc4459”。此信息不是很有用,并且可能会在程序执行之间发生变化。
如果要显示数组的内容,则必须迭代它。
This code prints an array (byte[]), but there is no standard array printing in Java. So instead of printing the content of the array, the code displays some cryptic memory reference to the array. Eg "[B@6bbc4459". This information is not very useful and is likely to change between programm executions.
If you want to display the content of the array, you must iterate through it.
您正在打印对字节数组调用
toString()
的结果。 不会向您显示内容,因为数组不会覆盖toString()
- 它只是向您显示类似[B@ABCDEF01
其中[B
表明它是一个字节数组,@后面的值是一个哈希码。如果要将字节数组内容显示为数字,则需要类似
Arrays.toString
:You're printing the result of calling
toString()
on a byte array. That doesn't show you the contents, as arrays don't overridetoString()
- it's just showing you something like[B@ABCDEF01
where the[B
shows that it's a byte array, and the value after the @ is a hash code.If you want to show the byte array contents as numbers, you want something like
Arrays.toString
: