Have you tried using putChar method of ByteBuffer?
ByteBuffer buf = ByteBuffer.allocate(1024);
for (char ch : myChars) {
buf.putChar(ch);
}
سؤال
how to write char array to java socketchannel. I am not able to write the char data to socketchannel as it takes ByteBuffer.
المحلول
Have you tried using putChar method of ByteBuffer?
ByteBuffer buf = ByteBuffer.allocate(1024);
for (char ch : myChars) {
buf.putChar(ch);
}
نصائح أخرى
Several ways eg
char[] c = {'1', '2'};
String str = new String(c);
ByteBuffer bb = Charset.defaultCharset().encode(str);
or
ByteBuffer.wrap(str.getBytes());