Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

passing hex to FileOutputStream write methods  RSS feed

 
augustine mellor
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code creates a file and writes two values to it. The values, however, are different:
first: 0xaabbccdd (-143053899 as an int)
second: 0xdd (221 as an int)
However, when written to the file using write(somehex), the output to the file is IDENTICAL in value! How is this possible?
(incidentally, this is based on a practice question for the programmers certification exam)
the code:
import java.io.*;
public class RadixConv {
public static void main(String[] args) throws IOException {
File file = new File("RadixConv.vim");
FileOutputStream fos = new FileOutputStream(file);

fos.write(0xaabbccdd);
fos.write(0xdd);
fos.close();
}
}

thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API for the OutputStream write(int) method:
The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Since both your value have the same final 8 bits, they both write() the same value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!