Get byte array from string

ReturnMethodSummary
byte[]getBytes()Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.
byte[]getBytes(Charset charset)Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
byte[]getBytes(String charsetName)Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

import java.util.Arrays;


public class Main {
  public static void main(String[] argv) {
    String str = "     j a v a 2 s.com    ";
    byte[] bytes = str.getBytes();
    System.out.println(Arrays.toString(bytes));
  }
}

The output:


[32, 32, 32, 32, 32, 106, 32, 97, 32, 118, 32, 97, 32, 50, 32, 115, 46, 99, 111, 109, 32, 32, 32, 32]
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.