J2ME中如何从字节中获取int?
我检索了 LWUIT Button 的 backgroundTransparency
,它返回一个 byte
数据类型数据。我希望将此 byte
变量转换为 int
变量。怎么办呢?
I retrieved the backgroundTransparency
of a LWUIT Button , and it returns a byte
datatype data. I want this byte
variable to be converted to an int
variable. How to do that ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
J2ME 仍然是 Java:
J2ME is still Java:
Fernando 的答案 100% 正确,但仍然略有误导,例如:
乍一看这可能会让大多数人感到惊讶,但逻辑实际上很简单。 0xff 对于字节来说是负数,对于 int 来说是正数(在 Java SE 中也是如此)。解决方案是将上面的代码更改为“正确”转换为 int 的代码:
这将解决那里的问题,但您仍然应该注意:
Fernando's answer is 100% correct but is still slightly misleading e.g.:
That might surprise most people at first glance but the logic is actually simple. 0xff is a negative number for a byte but a positive number for an int (this is also true in Java SE). The solution is to change the code from above to something that will convert to int "properly":
This will solve the issue there but you should still be aware that: