Convert char value to byte array in Java
Description
The following code shows how to convert char value to byte array.
Example
// w w w.j a v a 2 s . c om
import java.nio.ByteBuffer;
public class Main {
public static byte[] convertToByteArray(char value) {
byte[] bytes = new byte[2];
ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
buffer.putChar(value);
return buffer.array();
}
public static void main(String[] argv){
System.out.println(convertToByteArray('a'));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »