public class CharToByte {
public static void fillInBytes(byte[] bytes, int index, char c) {
bytes[index] = (byte) ((c >>> 8) & 0x00FF);
bytes[index + 1] = (byte) (c & 0x00FF);
}
public static byte[] toBytes(char[] chars) {
return toBytes(chars, 0, chars.length);
}
public static byte[] toBytes(char[] chars, int off, int len) {
byte[] bytes = new byte[len << 1];
for (int i = 0; i < len; i++) {
int index = i << 1;
char c = chars[off + i];
bytes[index] = (byte) ((c >>> 8) & 0x00FF);
bytes[index + 1] = (byte) ((c & 0x00FF));
}
return bytes;
}
public static byte[] toBytes(CharSequence cs, int off, int len) {
byte[] bytes = new byte[len << 1];
for (int i = 0; i < len; i++) {
int index = i << 1;
char c = cs.charAt(off + i);
bytes[index] = (byte) ((c >>> 8) & 0x00FF);
bytes[index + 1] = (byte) ((c & 0x00FF));
}
return bytes;
}
public static byte[] toAsciiBytes(char[] chars) {
int length = chars.length;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = (byte) ((chars[i] & 0x00FF));
}
return bytes;
}
}