This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
The byte will contain character codes for the characters in the String, using some default encoding which will vary from country to country. For many installations, you will indeed effectively get the ASCII codes for the characters 1, 2, 3, 4, and 5: 49, 50, 51, 52, 53. If you want the characters themselves, use toCharArray().
You get different values from the getBytes() method depending on the specific character encoding you are using. Because that's what getBytes does, that's to say, assign one or more bytes to represent a character depending of the encoding.
ASCII values fit in one byte, but other kind of character encodings contain more than 256 characters. Like the unicode set, for instance. Hence, Java has to manipulate the corresponding characters using more than one byte.
You can determine the current character encoding settings by means of the System class:
If you use ASCII the generated bytes will correspond with the ASCII character numbers, it means that if you assign every byte of the array to a char variable you will get the corresponding ASCII charactrer back again:
But it will print unknow characters if you use another encoding. [ May 26, 2006: Message edited by: Edwin Dalorzo ]