Answers for "how to create an encoder and decoder in java"

1

how to create an encoder and decoder in java

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Scanner;

public class EncoderDecoder {

    public static String encode(String str){
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] encoded = encoder.encode(str.getBytes(StandardCharsets.UTF_8));
        return new String(encoded);
    }
    public static String decode(String str){
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] decoded = decoder.decode(str);
        return new String(decoded);
    }

    public static void main(String[] args) {
        String originalStr;
        Scanner str = new Scanner(System.in);
        Scanner x = new Scanner(System.in);
        System.out.println("==================Menu==================== ");
        System.out.println("Select your desired option: ");
        System.out.println("1) Encoding ");
        System.out.println("2) Decoding ");
        int choice = x.nextInt();

        switch (choice) {
            case 1 -> {
                System.out.println("Enter words to be encoded: ");
                originalStr = str.nextLine();
                String encodedStr = encode(originalStr);
                System.out.println("Encoded string: " + encodedStr);
            }
            case 2 -> {
                System.out.println("Enter words to be decoded: ");
                originalStr = str.nextLine();
                String decodedStr = decode(originalStr);
                System.out.println("Decoded String: " + decodedStr);
            }
        }

    }
}
//Taizya was here
Posted by: Guest on October-23-2021

Code answers related to "how to create an encoder and decoder in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language