getBytes() method in Java is defined as “converting the string into a byte of array”. getBytes() method returns byte[] array. getBytes() method in Java is applied with Strings only. Every string value in a byte array is converted into its equivalent ASCII (American Standard Code for Information Interchange) values. We can say it is an encoded form of the data to protect from unknown actions on the data.
Real-time Application: When we want to convert the string into a byte array, then the getBytes() method comes into the picture.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Java getBytes() method works are based on string values in 3 ways:
Syntax:
String str="Some String"; Byte[] byteArray=Str.getBytes();
Syntax:
String str=”UTF-16”; Byte[] byteArray=Str.getBytes();
Syntax:
Byte[] byteArray=Str.getBytes(“ASCII”);
Given below are the examples of Java String getBytes:
public byte[] getBytes()
Code:
package com.getbytes; public class GetBytesOfNames { public static void main(String args[]) { String name = "Paramesh"; /// defining a string byte[] nameByteArray = name.getBytes(); // converting string into byte array for (int i = 0; i < nameByteArray.length; i++) {// iterating byte array values System.out.println(name.charAt(i) + " character byte value =>" + nameByteArray[i]);// displaying values } } }
Output:
Explanation:
public byte[] getBytes()
Code:
package com.getbytes; public class GetBytesOfSpaceValues{ public static void main(String args[]) { String name = "This is Amardeep"; /// defining a string byte[] nameByteArray = name.getBytes(); // converting string into byte array for (int i = 0; i < nameByteArray.length; i++) {// iterating byte array values System.out.println(name.charAt(i) + " character byte value =>" + nameByteArray[i]);// displaying values } } }
Output:
Explanation:
public byte[] getBytes(String string)
Code:
package com.getbytes; import java.io.UnsupportedEncodingException; public class GetBytesString { public static void main(String args[]) { String name = "Amardeep"; // defining a string byte[] nameByteArray; try { nameByteArray = name.getBytes("UTF-8"); for (int i = 0; i < nameByteArray.length; i++) {// iterating byte array values System.out.println(name.charAt(i) + " character byte value =>" + nameByteArray[i]);// displaying values } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // converting string into byte array } }
Output:
Explanation:
public byte[] getBytes(String string)
Code:
package com.getbytes; import java.io.UnsupportedEncodingException; public class GetBytesOfNames { public static void main(String args[]) { String name = "Amardeep is smart"; // defining a string byte[] nameByteArray; try { nameByteArray = name.getBytes("UTF-8"); System.out.println("=========Coverting String into byte[] array========"); for (int i = 0; i < nameByteArray.length; i++) {// iterating byte array values System.out.println(name.charAt(i) + " character byte value =>" + nameByteArray[i]);// displaying values } String string=new String(nameByteArray);//converting byte array into string System.out.println("=========Coverting byte[] into original string========"); System.out.println(string); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } // converting string into byte array
Output:
Explanation:
public byte[] getBytes(Charset characterSet)
Code:
package com.getbytes; import java.nio.charset.Charset; public class GetBytesCharSet { public static void main(String args[]) { String name = "Hello"; // defining a string byte[] nameByteArray = name.getBytes(Charset.forName("ASCII")); System.out.println("=========Coverting String into byte[] array========"); for (int i = 0; i < nameByteArray.length; i++) {// iterating byte array values System.out.println(name.charAt(i) + " character byte value =>" + nameByteArray[i]);// displaying values } } }
Output:
Explanation:
public byte[] getBytes(Charset characterSet)
Code:
package com.getbytes; import java.nio.charset.Charset; public class GetBytesOfCharSetAndString { public static void main(String args[]) { String name = "Hello Amardeep"; // defining a string byte[] nameByteArray = name.getBytes(Charset.forName("ASCII")); System.out.println("=========Coverting String into byte[] array========"); for (int i = 0; i < nameByteArray.length; i++) {// iterating byte array values System.out.println(name.charAt(i) + " character byte value =>" + nameByteArray[i]);// displaying values } String string=new String(nameByteArray); System.out.println("=========Coverting String into byte[] array========"); System.out.println(string); } }
Output:
Explanation:
String can be converted into its equivalent ASCII code, UTF code, ISO code based on getBytes(), getBytes(String string) and getBytes(Charset characterSet).
The above is the detailed content of Java String getBytes. For more information, please follow other related articles on the PHP Chinese website!