Java.lang.String.getByte() in Java
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()publicclassGetByte {publicstaticvoidmain(String args[]){// Initializing StringString gfg ="ASTHA GFG";// Displaying string values before// conversionSystem.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 conversionSystem.out.println("The String after conversion is : ");for(inti =0; i < b.length; i++) {System.out.print(b[i]);}}}chevron_rightfilter_noneOutput:
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 setsimportjava.io.*;publicclassGetBytecharset {publicstaticvoidmain(String args[]){// Initializing StringString gfg =newString("ASTHA GFG");// Displaying string values before// conversionSystem.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-16System.out.println("The String after conversion into UTF-16 is : ");for(inti =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-16BESystem.out.println("The String after conversion into UTF-16BE is : ");for(inti =0; i < c.length; i++) {System.out.print(c[i]);}}catch(UnsupportedEncodingException g) {System.out.println("Unsupported character set"+ g);}}}chevron_rightfilter_noneOutput:
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.
Recommended Posts:
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.concurrent.Phaser class in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.lang.Short toString() method in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2



