Home >> Core Java >> lang >> String >> Convert String to byte array ASCII encoding


Convert String to byte array ASCII encoding


public static byte[] stringToBytesASCII(String str) {
 byte[] b = new byte[str.length()];
 for (int i = 0; i < b.length; i++) {
  b[i] = (byte) str.charAt(i);
 }
 return b;
}
Using the resulted byte array we can convert back to the original String, by utilizing the “classic” String constructor “new String(byte[])

Related Article: