Java tutorial
//package com.java2s; //License from project: Open Source License import java.io.UnsupportedEncodingException; public class Main { public static byte[] string2Bytes(String s) { return string2Bytes(s, null); } public static byte[] string2Bytes(String s, String charsetName) { if (s == null) { return null; } if (charsetName == null) { return s.getBytes(); } else { try { return s.getBytes(charsetName); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } } }