This method has two variants :
public byte[] getBytes()
It encodes the string into sequence of bytes using default charset.
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
It encodes the string into sequence of bytes using the specified charset. This method replaces malformed-input and unmappable-character sequences with the specified charset's default replacement. This method throws an exception, and it should be surrounded with try-catch block.
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");
}
}
}
OUTPUTAfter 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
DESCRIPTIONAt 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
.
Usually, after converting to byte array they are passed to input streams for writing to files or sending over the network.