23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

int unsignedByte = signedByte >= 0 ? signedByte : 256 + signedByte;<br />

1.2.2 Conversions and Casts<br />

<strong>Java</strong> I/O<br />

Since bytes have such a small range, they're often converted to ints in calculations and<br />

method invocations. Often they need to be converted back, generally through a cast.<br />

Therefore, it's useful to have a good grasp of exactly how the conversion occurs.<br />

Casting from an int to a byte—for that matter, casting from any wider integer type to a<br />

narrower type—takes place through truncation of the high-order bytes. This means that as<br />

long as the value of the wider type can be expressed in the narrower type, the value is not<br />

changed. The int 127 cast to a byte still retains the value 127.<br />

On the other hand, if the int value is too large for a byte, strange things happen. The int 128<br />

cast to a byte is not 127, the nearest byte value. Instead, it is -128. This occurs through the<br />

wonders of two's complement arithmetic. Written in hexadecimal, 128 is 0x00000080. When<br />

that int is cast to a byte, the leading zeros are truncated, leaving 0x80. In binary this can be<br />

written as 10000000. If this were an unsigned number, 10000000 would be 128 and all would<br />

be fine, but this isn't an unsigned number. Instead, the leading bit is a sign bit, and that 1 does<br />

not indicate 2 7 but a minus sign. The absolute value of a negative number is found by taking<br />

the complement (changing all the 1 bits to bits and vice versa) and adding 1. The complement<br />

of 10000000 is 01111111. Adding 1, you have 01111111 + 1 = 10000000 = 128 (decimal).<br />

Therefore, the byte 0x80 actually represents -128. Similar calculations show that the int 129<br />

is cast to the byte -127, the int 130 is cast to the byte -126, the int 131 is cast to the byte -<br />

125, and so on. This continues through the int 255, which is cast to the byte -1.<br />

When 256 is reached, the low-order bytes of the int are now filled with zeros. In other words,<br />

256 is 0x00000100. Thus casting it to a byte produces 0, and the cycle starts over. This<br />

behavior can be reproduced algorithmically with this formula, though a cast is obviously<br />

simpler:<br />

int byteValue;<br />

int temp = intValue % 256;<br />

if ( intValue < 0) {<br />

byteValue = temp < -128 ? 256 + temp : temp;<br />

}<br />

else {<br />

byteValue = temp > 127 ? temp - 256 : temp;<br />

}<br />

1.3 Character Data<br />

Numbers are only part of the data a typical <strong>Java</strong> program needs to read and write. Most<br />

programs also need to handle text, which is composed of characters. Since computers only<br />

really understand numbers, characters are encoded by matching each character in a given<br />

script to a particular number. For example, in the common ASCII encoding, the character A is<br />

mapped to the number 65; the character B is mapped to the number 66; the character C is<br />

mapped to the number 67; and so on. Different encodings may encode different scripts or may<br />

encode the same or similar scripts in different ways.<br />

20

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!