outputStream.write() for int[] array

怎甘沉沦 提交于 2019-12-12 04:18:00

问题


I'm looking for something that does the same thing than outputStream.write() but which will accept an array of int.

Actually, I'm using this one : outputStream.write() but this one accepts only byte,byte[] or int.

I could use the byte[] but the values I want to send are

 [255,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255]

so I can use the byte[] because there range are only from -127 to 127 :/

It's to send a command on a Port_Com which accept only packet of 19 bytes and must be start and end with 255.


回答1:


This is a common misconception about bytes, because of rumors repeated again and again.

Actually, the range of byte is from

00000000 (binary) to   11111111 (binary)

There is no reason to interpret bytes as numbers, if you're only interested in the bit patterns. There is, in particular, no reason to interpret bytes as signed numbers, just because java does it that way by default.

Hence, go ahead, as Jon Skeet says, cast your integers to byte and write those bytes.



来源:https://stackoverflow.com/questions/22474001/outputstream-write-for-int-array

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