J2ME中如何从字节中获取int?

发布于 2024-12-09 01:05:30 字数 150 浏览 2 评论 0原文

我检索了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情释 2024-12-16 01:05:30

J2ME 仍然是 Java:

int intVar = byteBar;

J2ME is still Java:

int intVar = byteBar;
橪书 2024-12-16 01:05:30

Fernando 的答案 100% 正确,但仍然略有误导,例如:

 byte b = (byte)0xff;
 int intVar = b;

 boolean thisIsFalse = intVar == 0xff;

乍一看这可能会让大多数人感到惊讶,但逻辑实际上很简单。 0xff 对于字节来说是负数,对于 int 来说是正数(在 Java SE 中也是如此)。解决方案是将上面的代码更改为“正确”转换为 int 的代码:

 int intVal = b & 0xff;
 boolean thisIsTrue = intVar == 0xff;

这将解决那里的问题,但您仍然应该注意:

 boolean thisIsFalse = intVar == b;
 boolean thisIsTrue = intVar == (b & 0xff);

Fernando's answer is 100% correct but is still slightly misleading e.g.:

 byte b = (byte)0xff;
 int intVar = b;

 boolean thisIsFalse = intVar == 0xff;

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":

 int intVal = b & 0xff;
 boolean thisIsTrue = intVar == 0xff;

This will solve the issue there but you should still be aware that:

 boolean thisIsFalse = intVar == b;
 boolean thisIsTrue = intVar == (b & 0xff);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文