Java examples for java.lang:byte Array Convert
convert string to Byte array without encoding
//package com.java2s; public class Main { public static void main(String[] argv) { String string = "java2s.com"; int bytesPerChar = 42; System.out.println(java.util.Arrays.toString(getBytes(string, bytesPerChar)));//from w w w. j a v a 2 s .c o m } public static byte[] getBytes(final String string, final int bytesPerChar) { char[] chars = string.toCharArray(); byte[] toReturn = new byte[chars.length * bytesPerChar]; for (int i = 0; i < chars.length; i++) { for (int j = 0; j < bytesPerChar; j++) toReturn[i * bytesPerChar + j] = (byte) (chars[i] >>> (8 * (bytesPerChar - 1 - j))); } return toReturn; } }