Here you can find the source of charArrayToByteArray(char[] c)
static byte[] charArrayToByteArray(char[] c)
//package com.java2s; /*/*from w w w.java 2 s .c o m*/ * @(#)NodeUtil.java 1.2 05/06/27 * * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. * * See the file "LICENSE.txt" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. */ public class Main { static byte[] charArrayToByteArray(char[] c) { int i = 1; int j = 0; int len = c.length; if (c[0] == '\0') { // even length byte[] bytes = new byte[(len - 1) * 2]; for (; i < len; i++) { char ch = c[i]; bytes[j++] = (byte) ((ch >> 8) & 255); bytes[j++] = (byte) (ch & 255); } return bytes; } else { // odd length byte[] bytes = new byte[(len - 1) * 2 - 1]; for (; i < len - 1; i++) { char ch = c[i]; bytes[j++] = (byte) ((ch >> 8) & 255); bytes[j++] = (byte) (ch & 255); } bytes[j++] = (byte) ((c[i] >> 8) % 255); return bytes; } } }