表示字节数组中的数字(java编程)
我试图在两字节数组中表示端口号 9876(或十六进制的 0x2694):
class foo {
public static void main (String args[]) {
byte[] sendData = new byte[1];
sendData[0] = 0x26;
sendData[1] = 0x94;
}
}
但我收到有关可能丢失精度的警告:
foo.java:5: possible loss of precision
found : int
required: byte
sendData[1] = 0x94;
^
1 error
如何在两字节数组中表示数字 9876 而不会丢失精度?
注意:我选择了 @Björn 的代码作为正确答案,但 @glowcoder 的代码也运行良好。这只是解决同一问题的不同方法。谢谢大家!
I'm trying to represent the port number 9876 (or 0x2694 in hex) in a two byte array:
class foo {
public static void main (String args[]) {
byte[] sendData = new byte[1];
sendData[0] = 0x26;
sendData[1] = 0x94;
}
}
But I get a warning about possible loss of precision:
foo.java:5: possible loss of precision
found : int
required: byte
sendData[1] = 0x94;
^
1 error
How can I represent the number 9876 in a two byte array without losing precision?
NOTE: I selected the code by @Björn as the correct answer, but the code by @glowcoder also works well. It's just a different approach to the same problem. Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您是否尝试过转换为字节?例如
Have you tried casting to a byte ? e.g.
0x94十进制表示为148,超出了java中byte的范围(-128到127)。
您可以执行以下操作之一:
1) 强制转换可以正常工作,因为它将保留二进制表示形式(0x00 到 0xFF 不会截断任何有意义的位):
2) 0x94 作为有符号字节的二进制表示形式是 -108 (- 0x6C),因此以下内容将具有相同的效果:
0x94 is 148 in decimal, which exceeds the range of byte in java (-128 to 127).
You can do one of the following:
1) A cast will work fine because it will preserve the binary representation (no meaningful bits are truncated for 0x00 to 0xFF):
2) The binary representation of 0x94 as a signed byte is -108 (-0x6C), so the following will have the same effect:
Björn 对于使用流给出了一个很好的通用答案。您还可以使用 java.nio.ByteBuffer 执行相同的操作,这会导致代码稍微少一些,并且您还可以控制输出的字节顺序(字节顺序)。
创建字节数组:
反转它:
Björn gave a good generic answer with using streams. You can also do this same thing using
java.nio.ByteBuffer
which results in slightly less code and you could also control endianess (byte order) of the output.To create the byte array:
To reverse it:
我的第一个答案是位移,但转念一想,我认为使用输出流可能更好、更容易理解。我通常会避免强制转换,但如果您不寻求通用解决方案,我想那也没关系。 :)
使用流,一个通用的解决方案:
并反转它:
My first answer would be bitshifting, but on a second thought I think using outputstreams could be better and more simple to understand. I usually avoid casting, but if you're not going for a generic solution I guess that would be okay. :)
Using streams, a generic solution:
And to reverse it:
你必须转换为(byte),因为java中默认的数字类型是int,它比byte大。只要该值适合字节就可以进行转换。
You have to cast to (byte) as default number type in java is int, which is bigger than byte. As long as the value fits in byte it is ok to cast.
试试这个:
或者这个:
您必须将数据转换为字节才能将其分配给字节!
这并不意味着你失去了精度,仅仅写
0x26
就意味着 Java 编译器的 int 。但还要注意:字节的范围是从 -128 到 127,所以在
0x94= 的情况下148
字节转换后它将表示为 '-108' ,因此它在数学计算中将无法正常工作。Try this:
Or this:
You must cast data into byte in order to assign it to a byte!
That does not mean you lost precision, just writing
0x26
means an int to Java compiler..But also note: range of a byte is from -128 to 127, so in case of
0x94=148
it will be represented after byte casting as '-108' , so it will not work correctly in mathematical computations..这是因为 Java 中的所有内容都是有签名的。 0x94 (148) 大于 Byte.MAX_VALUE(2^7-1)。
你需要的是
It's because everything in Java is signed. 0x94 (148) is greater than Byte.MAX_VALUE(2^7-1).
What you need is