This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read/Write ints using 2bytes" Watch "Read/Write ints using 2bytes" New topic

Read/Write ints using 2bytes

Lora Tully

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...


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


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

Joined: Nov 29, 2004
Posts: 8
Thanks that works great!
subject: Read/Write ints using 2bytes
Similar Threads
any help on reading data please
i/o streams and
Read and load xml file into a db table using SAX
B&S: Data File Format
Writing (INSERTING) to a previously created text file