|
Do you need help with your Java programming?
Click here for instant help with your Java code. |
Convert String to byte array
This little code example shows a String to byte array conversion. The String class has a method called getBytes which returns an array of bytes. In the example the length of the array is printed out to illustrate that the length of the array is the same as the number of characters in the String. |
/** * * @author www.javadb.com */ 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 |
Check out this link to convert byte array to String |
| Do you know your Java? | |
| Take a Ten-Question-Java-Quiz! | |
Search for code examples on this site
