This tutorial highlights the simple example to demonstrates how to convert a string object to the byte stream. We can simply use the getBytes() method in the string object to convert the string to bytes array. Lets look at the example program.
[code lang=”java”]
package javabeat.net.core;
public class StringToByteExample {
public static void main(String[] argv) {
String str = "JavaBeat Website";
byte[] bytes = str.getBytes();
System.out.println("String Value : " + str);
System.out.println("String [Byte Format] : " + bytes);
System.out.println("—–Printing Bytes Array——-");
for (int i=0;i<bytes.length;i++){
System.out.println(bytes[i]);
}
}
}
[/code]
Output
[code]
String Value : JavaBeat Website
String [Byte Format] : [B@15f5897
—–Printing Bytes Array——-
74
97
118
97
66
101
97
116
32
87
101
98
115
105
116
101
[/code]
Leave a Reply