News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

Java String getBytes(Charset charset) Example

Started by VelMurugan, Nov 26, 2008, 06:50 PM

Previous topic - Next topic

VelMurugan

Java String getBytes(Charset charset) Example

String class getBytes(Charset charset) method example

String class getBytes(Charset charset) method example. This example shows you how to use getBytes(Charset charset) method.This method Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.

Here is the Code:-

/**
* @(#) GetBytes1string.java
*Program that Convert this String into bytes according to the specified character encoding, storing the result into a new byte array.
* @Version  01-May-2008
* @author   Rose India Team
*/
public class GetBytes1string
{
    public static void main(String[] args)
    {
    try
    {
         String str = "Welcome to RoseIndia.";
            // 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 (Exception ex)
    {
    }
    }
}

Source : CodeDiary

unselmlaneurn

'Natural disasters' bring out the best in all of us, especially the United States. This weekend, a few Penn State students will join nearly 10,000 others from across the country to rally in the nation’s capital at this year’s Power Shift 2011. FLORENCE (AP) - Cracks have opened in the Florence High School cafeteria and gymnasium and in its athletic stadium.