Integer.MIN_VALUE怎么转为long 4294967295?

lxyscls 2017-04-06 07:28:33
Integer.MIN_VALUE = -2147483648,如果是无符号 = 4294967295。
long l = (long) Integer.MIN_VALUE;

这种自动补1,l还是等于-2147483648。请问有没有什么简单的方法将Integer.MIN_VALUE赋给long/Long,但是前面不补1的。
...全文
305 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxyscls 2017-04-07
  • 打赏
  • 举报
回复
看来DataInputStream多余了,直接用ByteArrayInputStream.
lxyscls 2017-04-06
  • 打赏
  • 举报
回复
引用 1 楼 u013155549 的回复:
= -2147483648 int 转long 向上转型
Integer.MIN_VALUE向上转型会把第一个符号位给自动往上补的,值还是-2147483648。 或者这么讲吧:Integer.MIN_VALUE的底层是0x80,00,00,00,向上转型成了0xff,ff,ff,ff,80,00,00,00,而我想要的还是0x80,00,00,00
小七002 2017-04-06
  • 打赏
  • 举报
回复
= -2147483648 int 转long 向上转型
lxyscls 2017-04-06
  • 打赏
  • 举报
回复
神了,自己还不能编辑自己的帖子了 补充:输入为大端序byte[],但是需要表示为无符号数,java不支持 目前考虑的方法是:
InputStream is = new DataInputStream(new ByteArrayInputStream(buf));
byte[] bb = new byte[2]; // 针对byte
byte[] bs = new byte[4]; // 针对short
byte[] bi = new byte[8]; // 针对int

is.read(bb, 1, 1);
is.read(bs, 2, 2);
is.read(bi, 4, 4);

short s = new BigInteger(bb).shortValue(); // byte -> short
int i = new BigInteger(bs).intValue(); // short -> int
long l = new BigInteger(bi).longValue(); // int -> long
土?还有更简洁的方法吗?

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧