getbytes() function in java is used to convert a string into sequence of bytes and returns an array of bytes. This function can be implemented in two ways. Both the ways are discussed in this article.
- Syntax 1 – public byte[] getBytes() : This function takes no arguments and used default charset to encode the string into bytes.
// Java code to demonstrate the working of
// getByte()
public
class
GetByte {
public
static
void
main(String args[])
{
// Initializing String
String gfg =
"ASTHA GFG"
;
// Displaying string values before
// conversion
System.out.println(
"The String before conversion is : "
);
System.out.println(gfg);
// converting the string into byte
// using getBytes ( converts into ASCII values )
byte
[] b = gfg.getBytes();
// Displaying converted string after conversion
System.out.println(
"The String after conversion is : "
);
for
(
int
i =
0
; i < b.length; i++) {
System.out.print(b[i]);
}
}
}
Output:
The String before conversion is : ASTHA GFG The String after conversion is : 658384726532717071
- Syntax 2 : public byte[] getBytes(Charset charset): This implementation accepts the charset according to which string has to be encoded while conversion into bytes. There are many charset defined and are discussed below.
- US-ASCII: Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
- ISO-8859-1: ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
- UTF-8: Eight-bit UCS Transformation Format
- UTF-16BE: Sixteen-bit UCS Transformation Format, big-endian byte order
- UTF-16LE: Sixteen-bit UCS Transformation Format, little-endian byte order
- UTF-16: Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark.
// Java code to demonstrate the working of
// getByte() using different character sets
import
java.io.*;
public
class
GetBytecharset {
public
static
void
main(String args[])
{
// Initializing String
String gfg =
new
String(
"ASTHA GFG"
);
// Displaying string values before
// conversion
System.out.println(
"The String before conversion is : "
);
System.out.println(gfg);
try
{
// converting the string into byte
// using getBytes ( converts into UTF-16 values )
byte
[] b = gfg.getBytes(
"UTF-16"
);
// Displaying converted string after conversion
// into UTF-16
System.out.println(
"The String after conversion into UTF-16 is : "
);
for
(
int
i =
0
; i < b.length; i++) {
System.out.print(b[i]);
}
System.out.print(
"\n"
);
// converting the string into byte
// using getBytes ( converts into UTF-16BE values )
byte
[] c = gfg.getBytes(
"UTF-16BE"
);
// Displaying converted string after conversion
// into UTF-16BE
System.out.println(
"The String after conversion into UTF-16BE is : "
);
for
(
int
i =
0
; i < c.length; i++) {
System.out.print(c[i]);
}
}
catch
(UnsupportedEncodingException g) {
System.out.println(
"Unsupported character set"
+ g);
}
}
}
Output:
The String before conversion is : ASTHA GFG The String after conversion into UTF-16 is : -2-1065083084072065032071070071 The String after conversion into UTF-16BE is : 065083084072065032071070071
This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.