Export (0) Print
Expand All
Expand Minimize

java.lang.String.getBytes(String enc)

Visual Studio 2005

Gets the contents of the String as a byte array using the given encoding.


public byte[] getBytes(String enc) throws UnsupportedEncodingException

Parameters

Parameter Description

enc

The encoding for the contents of the newly created byte array.

A byte array containing the contents of the current String.

// string_getbytes2.jsl

import java.io.UnsupportedEncodingException;

public class Program
{
    public static void main(String[] args)
    {
        String str = "This is a String.";

        try
        {
            // Copy the contents of the String to a byte array
            // using the ASCII encoding.
            byte[] arr = str.getBytes("ASCII");

            // Create a new String using the contents of the byte array.
            String newStr = new String(arr);

            // Display the contents of the byte array.
            System.out.println("The new String equals \"" +
                newStr + "\"");
        }
        catch (UnsupportedEncodingException ex)
        {
            System.out.println(ex.toString());
        }
    }
}

/*
Output:
The new String equals "This is a String."
*/

Community Additions

ADD
Show:
© 2014 Microsoft