Here you can find the source of toBytes(String str, boolean wideChar)
public static byte[] toBytes(String str, boolean wideChar)
//package com.java2s; /******************************************************************************* * This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * //from w ww.ja v a 2 s . c o m * Contributors: * Peter Smith *******************************************************************************/ import java.nio.ByteBuffer; import java.nio.ByteOrder; public class Main { public static byte[] toBytes(String str, boolean wideChar) { int len = str.length() + 1; if (wideChar) len <<= 1; byte[] buf = new byte[len]; ByteBuffer bb = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN); for (int i = 0; i < str.length(); i++) { if (wideChar) bb.putChar(str.charAt(i)); else bb.put((byte) str.charAt(i)); } if (wideChar) bb.putChar((char) 0); else bb.put((byte) 0); return buf; } }