String convert byte array char array

String convert byte array char array

String convert byte array char array

Summary: Many a times it is required to convert the string characters into a char array or byte array; of course, very often, the reverse is also required. Discussed at length in this tutorial "String convert byte array char array".

Program converting strings to char array and byte array

Methods toCharArray() and getBytes() of String class are used.


String convert byte array char array
Output screen of StringToArray.java of String convert byte array char array

Two methods of String class toCharArray() and getBytes() are used here. toCharArray() returns a char array that contains the elements nothing but the characters present in the string itself. Similarly, the getBytes() returns a byte array, the byte representation (ASCII values) of the characters present in the string. In a for loop the output is printed.

These methods are frequently by the Developer in coding.

The other methods of String class useful in these conversions are getChars() and overloaded copyValueOf().

The reverse is converting char array and byte array to string is available at Char array and Byte array to String Java.

Leave a Reply

Your email address will not be published. Required fields are marked *