Encode and Decode a String in Java - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Tuesday, 6 February 2018

Encode and Decode a String in Java

package practice;

import java.io.UnsupportedEncodingException;

import javax.xml.bind.DatatypeConverter;

public class test {

    public static void main(String[] args) throws UnsupportedEncodingException {

        String str = "77+9x6s=";
        // encode data using BASE64
      //printBase64Binary(byte[] val)
//This method converts an array of bytes into a string

      String encoded = DatatypeConverter.printBase64Binary(str.getBytes());//static String .
        System.out.println("encoded value is \t" + encoded);

        // Decode data
        String decoded = new String(DatatypeConverter.parseBase64Binary(encoded));
        System.out.println("decoded value is \t" + decoded);

        System.out.println("original value is \t" + str);
    }

}

No comments:

Post a Comment