Java Utililty Methods Char Array to Byte Array

List of utility methods to do Char Array to Byte Array

Description

The list of methods to do Char Array to Byte Array are organized into topic(s).

Method

byte[]charsToBytes(char[] in)
Note: only one-byte symbols are supported.
int size = in.length;
byte[] out = new byte[size];
for (int i = 0; i < size; i++) {
    out[i] = (in[i] < 256) ? (byte) in[i] : 63 ;
return out;
voidcharsToBytes(final char[] chars, final int charOffset, final int length, final byte[] bytes, final int byteOffset)
Convert chars to bytes merely by casting
for (int i = 0; i < length; ++i) {
    bytes[byteOffset + i] = (byte) chars[charOffset + i];
byte[]ConvertCharArrayToByteArray(char value[])
Converts a char array into a byte array by typecasting each char to a byte
byte returnValue[] = new byte[value.length];
int count = 0;
while (count < value.length) {
    returnValue[count] = (byte) value[count];
    count++;
return returnValue;
byte[]convertCharsToBytes(char[] chars)
Converts an array of unsigned 16bit numbers to an array of bytes.
byte[] result = new byte[chars.length * 2];
for (int i = 0; i < chars.length; i++) {
    result[2 * i] = (byte) (chars[i] / 256);
    result[2 * i + 1] = (byte) (chars[i] % 256);
return result;
byte[]convertCharsToBytes(char[] chars)
convert Chars To Bytes
byte[] result = new byte[chars.length * 2];
for (int i = 0; i < chars.length; ++i) {
    result[2 * i] = (byte) (chars[i] / 256);
    result[2 * i + 1] = (byte) (chars[i] % 256);
return result;