Menu
Topics Index
...
`

String Get Bytes Demo - Java Example Program


String Get Bytes Demo
class StringGetBytesDemo
{
    public static void main(String arg[])
    {
        String input = new String("Merit Campus");
        try {
            byte[] bytes = input.getBytes(); // LINE A
            System.out.print("After encoding : ");
            for (byte b : bytes)
                System.out.print(b + " ");
            System.out.println();
            System.out.print("After encoding : ");
            bytes = input.getBytes("UTF-8"); // LINE B
            for (byte b : bytes)
                System.out.print(b + " ");
            System.out.println();
            System.out.print("After encoding : ");
            bytes = input.getBytes("ISO-8859-1"); // LINE C
            for (byte b : bytes)
                System.out.print(b + " ");
        } catch (Exception e) {
            System.out.println("Unsupported character set");
        }    
    }
}
OUTPUT

After encoding : 77 101 114 105 116 32 67 97 109 112 117 115
After encoding : 77 101 114 105 116 32 67 97 109 112 117 115
After encoding : 77 101 114 105 116 32 67 97 109 112 117 115

DESCRIPTION

At LINE A, the given string "Merit Campus" is encoded using the default charset.
At LINE B, the given string "Merit Campus" is encoded using the "UTF-8" charset.
At LINE C, the given string "Merit Campus" is encoded using the "ISO-8859-1" charset.

THINGS TO TRY
  • Try encoding the string "Merit Campus ‰" with the above charsets.
  • Try encoding strings with different characters like "Learn", "Practice" and "Compete".
  • Try encoding with other standards like UTF-16, UTF-16BE, UTF-16LE, UTF-32, ISO-8859-2, ISO-8859-4, ISO-8859-5.

© meritcampus 2019

All Rights Reserved.

Open In App