Java read unsigned int, store, and write it back

守給你的承諾、 提交于 2019-12-04 19:42:12

Just write your long casted to int. I checked:


PipedOutputStream pipeOut = new PipedOutputStream ();
PipedInputStream pipeIn = new PipedInputStream (pipeOut);
DataOutputStream os = new DataOutputStream (pipeOut);

long uInt = 0xff1ffffdL;

System.out.println ("" + uInt + " vs " + ((int) uInt));
os.writeInt ((int) uInt);
for (int i = 0; i < 4; i++) System.out.println (pipeIn.read ());

uInt = 0x000ffffdL;
System.out.println ("" + uInt + " vs " + ((int) uInt));
os.writeInt ((int) uInt);
for (int i = 0; i < 4; i++) System.out.println (pipeIn.read ());

Output is

4280287229 vs -14680067
255
31
255
253
1048573 vs 1048573
0
15
255
253
as expected

If you only want to read, store and rewrite it, then you can just use int. More general: as long as you do not interpret the bits you can just read, store and write them without caring about the intended interpretation of the bits.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!