Convert String to byte array


public class Main {
    
    /*
     * This method converts a String to an array of bytes
     */
    public void convertStringToByteArray() {
        
        String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";
        
        byte[] theByteArray = stringToConvert.getBytes();
        
        System.out.println(theByteArray.length);
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().convertStringToByteArray();
    }
}


 

When executed, the code will output the length of the array: 76

0 comments:

                                                                

Site Meter