سؤال

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());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top