[ACCEPTED]-Representing char as a byte in Java-byte

Accepted answer
Score: 35

To convert characters to bytes, you need 14 to specify a character encoding. Some character encodings 13 use one byte per character, while others 12 use two or more bytes. In fact, for many 11 languages, there are far too many characters 10 to encode with a single byte.

In Java, the 9 simplest way to convert from characters 8 to bytes is with the String class's getBytes(Charset) method. (The 7 StandardCharsets class defines some common encodings.) However, this 6 method will silently replace characters 5 with � if the character cannot 4 be mapped under the specified encoding. If 3 you need more control, you can configure 2 a CharsetEncoder to handle this case with an error or 1 use a different replacement character.

Score: 9

A char is indeed 16 bits in Java (and is 20 also the only unsigned type!!).

If you are 19 sure the encoding of your characters is 18 ASCII, then you can just cast them away 17 on a byte (since ASCII uses only the lower 16 7 bits of the char).

If you do not need to 15 modify the characters, or understand their 14 signification within a String, you can just 13 store chars on two bytes, like:

char[] c = ...;
byte[] b = new byte[c.length*2];
for(int i=0; i<c.length; i++) {
    b[2*i] = (byte) (c[i]&0xFF00)>>8; 
    b[2*i+1] = (byte) (c[i]&0x00FF); 

(It may be 12 advisable to replace the 2* by a right shift, if 11 speed matters).

Note however that some actual 10 (displayed) characters (or, more accurately, Unicode 9 code-points) are written on two consecutive 8 chars. So cutting between two chars does 7 not ensure that you are cutting between 6 actual characters.

If you need to decode/encode 5 or otherwise manipulate your char array 4 in a String-aware manner, you should rather 3 try to decode and encode your char array 2 or String using the java.io tools, that 1 ensure proper character manipulation.

Score: 4

To extend what others are saying, if you 8 have a char that you need as a byte array, then 7 you first create a String containing that 6 char and then get the byte array from the 5 String:

private byte[] charToBytes(final char x) {
  String temp = new String(new char[] {x});
  try {
    return temp.getBytes("ISO-8859-1");
  } catch (UnsupportedEncodingException e) {
    // Log a complaint
    return null;

Of course, use the appropriate character 4 set. Much more efficient that this would 3 be to start working with Strings rather 2 than take a char at a time, convert to a 1 String, then convert to a byte array.

Score: 0

char in java is an unsigned 16 bit value. If 4 what you have will fit in 7 bits then just 3 do the cast to a byte (for instance ASCII 2 will fit).

You could checkout the java.nio.charset APIs as 1 well.

More Related questions