GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Read/Write ints using 2bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read/Write ints using 2bytes" Watch "Read/Write ints using 2bytes" New topic
Author

Read/Write ints using 2bytes

Lora Tully
Greenhorn

Joined: Nov 29, 2004
Posts: 8
Hi. I was wondering if anyone can help me read and write one interger using two bytes. I have an interger between 500-800 that I need to write. Normally I would use...

OUT.writeByte(int)

But as 500 is above one byte (256) I was wondering how to write this, and then later read it back.
As since one int would be spread over two bytes, I have no idea how to read it, normally I would use

IN.readByte()

And I have no idea how to do this. Can anyone help me please?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Try DataOutputStream.writeShort() and DataInputStream.readShort()


Mike Gershman
SCJP 1.4, SCWCD in process
Lora Tully
Greenhorn

Joined: Nov 29, 2004
Posts: 8
Thanks that works great!
 
Consider Paul's rocket mass heater.
 
subject: Read/Write ints using 2bytes