Java examples for Language Basics:StringBuffer
first convert StringBuffer to String object using toString method of String class.
the use byte[] getBytes() method of String class to convert it into byte array.
import java.util.Arrays; public class Main { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("this is a test"); byte bytes[] = sbf.toString().getBytes(); System.out.println(Arrays.toString(bytes)); // ww w . ja v a 2 s. c o m System.out.println("StringBuffer converted to byte array"); } }