- To convert byte array to string we have a constructor in String class which is taking byte array as an argument.
- So just we need to pass byte array object to string as argument while creating String class object.
- package com.instanceofjavaforus;
- public class ByteArrayToString {
- /*
- * This method converts a byte array to a String object.
- */
- public static void convertByteArrayToString() {
- byte[] byteArray = new byte[] {78,73, 67,69};
- String value = new String(byteArray);
- System.out.println(value);
- }
- public static void main(String[] args) {
- ByteArrayToString.convertByteArrayToString();
- }
- }
Output:
- NICE
Convert String to Byte Array:
- package com.instanceofjava;
- public class StringTOByteArray{
- /*
- * This example shows how to convert a String object to byte array.
- */
- public static void main(String[] args) {
- String data = "Instance of java";
- byte[] byteData = data.getBytes();
- System.out.println(byteData);
- }
- }
Output:
- [B@3b26456a
thanks for tutor, i can try this in my web
ReplyDeletehttp://informasipekerjaan.com
System.out.println prints the object reference. To print contents use System.out.write.
ReplyDelete