Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does writeBytes discard each character's high eight bits?

Tags:

java

I wanted to use DataOutputStream#writeBytes, but was running into errors. Description of writeBytes(String) from the Java Documentation:

Writes out the string to the underlying output stream as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits.

I think the problem I'm running into is due to the part about "discarding its high eight bits". What does that mean, and why does it work that way?

like image 899
asaini007 Avatar asked Nov 22 '13 17:11

asaini007


1 Answers

Most Western programmers tend to think in terms of ASCII, where one character equals one byte, but Java Strings are 16-bit Unicode. writeBytes just writes out the lower byte, which for ASCII/ISO-8859-1 is the "character" in the C sense.

like image 139
chrylis -cautiouslyoptimistic- Avatar answered Nov 15 '22 05:11

chrylis -cautiouslyoptimistic-