ADVERTISEMENT
ADVERTISEMENT

Home » Java programs » Java basic programs

Convert String to byte array and vice versa in java?

In this java program/example, we are converting String to byte array and byte array to String.
Submitted by Preeti Jain, on February 10, 2018

Example 1:

Java program to convert from String to byte array

class StringToByteArray{
	public static void main(String[] args){
		String s = "My Name Is Preeti Jain";
		byte[] byt = s.getBytes();
		System.out.println("String is converted in to byte[] :" + byt);
	}
}

Output

D:\Java Articles>java StringToByteArray
String is converted in to byte[] :[[email protected]

Example 2:

Java program to convert from byte array to String

class ByteArrayToString{
public static void main(String[] args){
	String qualification = "I have done MCA";
		byte[] byt = qualification.getBytes();
		String str = new String(byt);
		System.out.println("Byte[] array is converted into String :" + str);
	}
}

Output

D:\Java Articles>java ByteArrayToString
Byte[] array is converted into String :I have done MCA
ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.