how to convert StringBuffer to byte Array - Java Language Basics

Java examples for Language Basics:StringBuffer

Introduction

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.

Demo Code


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");
  }
}

Result


Related Tutorials