Decode an Encoded Base 64 String to ASCII String
Prerequisite : What is base64 Encoding and why we encode strings to base64 format
Base64 encoding is performed at sending node before transmitting bits over a network, and receiving node decodes that encoded data back to original ASCII string.
Base64 character set is
// 64 characters char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz0123456789+/"
Examples:
Input : TUVO04= // (Encoded into base64 format) Output : MENON // (Decoded back to ASCII string) Input : Z2Vla3Nmb3JnZWVrcw== Output : geeksforgeeks
Approach:
- Here each character in encoded string is considered to be made of 6 bits. We will take 4 characters each from Encoded String at one time i.e 4 * 6 = 24 bits. For each 4 characters of encoded string we will produce 3 characters of original string which will be of 8 bits each i.e 3 * 8 = 24 bits.
- Find their respective position in char_set and store it inside a variable (num) by using ‘|’ OR operator for storing bits and (LEFT – SHIFT) by 6 to make room for another 6 bits.
NOTE : We used ‘=’ in encoder to substitute for 2 missing bits, So here in decoder we have to reverse the process. Whenever we encounter a ‘=’ we have to delete 2 bits of num by using (RIGHT – SHIFT) by 2.
- After we have stored all the bits in num we will retrieve them in groups of 8, by using & operator with 255 (11111111), that will store the 8 bits from num and that will be our original character from ASCII string.
C++
// C++ Program to decode a base64// Encoded string back to ASCII string#include <bits/stdc++.h>using namespace std;#define SIZE 100/* char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */char* base64Decoder(char encoded[], int len_str){ char* decoded_string; decoded_string = (char*)malloc(sizeof(char) * SIZE); int i, j, k = 0; // stores the bitstream. int num = 0; // count_bits stores current // number of bits in num. int count_bits = 0; // selects 4 characters from // encoded string at a time. // find the position of each encoded // character in char_set and stores in num. for (i = 0; i < len_str; i += 4) { num = 0, count_bits = 0; for (j = 0; j < 4; j++) { // make space for 6 bits. if (encoded[i + j] != '=') { num = num << 6; count_bits += 6; } /* Finding the position of each encoded character in char_set and storing in "num", use OR '|' operator to store bits.*/ // encoded[i + j] = 'E', 'E' - 'A' = 5 // 'E' has 5th position in char_set. if (encoded[i + j] >= 'A' && encoded[i + j] <= 'Z') num = num | (encoded[i + j] - 'A'); // encoded[i + j] = 'e', 'e' - 'a' = 5, // 5 + 26 = 31, 'e' has 31st position in char_set. else if (encoded[i + j] >= 'a' && encoded[i + j] <= 'z') num = num | (encoded[i + j] - 'a' + 26); // encoded[i + j] = '8', '8' - '0' = 8 // 8 + 52 = 60, '8' has 60th position in char_set. else if (encoded[i + j] >= '0' && encoded[i + j] <= '9') num = num | (encoded[i + j] - '0' + 52); // '+' occurs in 62nd position in char_set. else if (encoded[i + j] == '+') num = num | 62; // '/' occurs in 63rd position in char_set. else if (encoded[i + j] == '/') num = num | 63; // ( str[i + j] == '=' ) remove 2 bits // to delete appended bits during encoding. else { num = num >> 2; count_bits -= 2; } } while (count_bits != 0) { count_bits -= 8; // 255 in binary is 11111111 decoded_string[k++] = (num >> count_bits) & 255; } } // place NULL character to mark end of string. decoded_string[k] = '\0'; return decoded_string;}// Driver codeint main(){ char encoded_string[] = "TUVOT04="; int len_str = sizeof(encoded_string) / sizeof(encoded_string[0]); // Do not count last NULL character. len_str -= 1; cout <<"Encoded string : " << encoded_string << endl; cout <<"Decoded string : " << base64Decoder(encoded_string, len_str) << endl; return 0;}// This code is contributed by// shubhamsingh10 |
C
// C Program to decode a base64// Encoded string back to ASCII string#include <stdio.h>#include <stdlib.h>#define SIZE 100/* char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */char* base64Decoder(char encoded[], int len_str){ char* decoded_string; decoded_string = (char*)malloc(sizeof(char) * SIZE); int i, j, k = 0; // stores the bitstream. int num = 0; // count_bits stores current // number of bits in num. int count_bits = 0; // selects 4 characters from // encoded string at a time. // find the position of each encoded // character in char_set and stores in num. for (i = 0; i < len_str; i += 4) { num = 0, count_bits = 0; for (j = 0; j < 4; j++) { // make space for 6 bits. if (encoded[i + j] != '=') { num = num << 6; count_bits += 6; } /* Finding the position of each encoded character in char_set and storing in "num", use OR '|' operator to store bits.*/ // encoded[i + j] = 'E', 'E' - 'A' = 5 // 'E' has 5th position in char_set. if (encoded[i + j] >= 'A' && encoded[i + j] <= 'Z') num = num | (encoded[i + j] - 'A'); // encoded[i + j] = 'e', 'e' - 'a' = 5, // 5 + 26 = 31, 'e' has 31st position in char_set. else if (encoded[i + j] >= 'a' && encoded[i + j] <= 'z') num = num | (encoded[i + j] - 'a' + 26); // encoded[i + j] = '8', '8' - '0' = 8 // 8 + 52 = 60, '8' has 60th position in char_set. else if (encoded[i + j] >= '0' && encoded[i + j] <= '9') num = num | (encoded[i + j] - '0' + 52); // '+' occurs in 62nd position in char_set. else if (encoded[i + j] == '+') num = num | 62; // '/' occurs in 63rd position in char_set. else if (encoded[i + j] == '/') num = num | 63; // ( str[i + j] == '=' ) remove 2 bits // to delete appended bits during encoding. else { num = num >> 2; count_bits -= 2; } } while (count_bits != 0) { count_bits -= 8; // 255 in binary is 11111111 decoded_string[k++] = (num >> count_bits) & 255; } } // place NULL character to mark end of string. decoded_string[k] = '\0'; return decoded_string;}// Driver functionint main(){ char encoded_string[] = "TUVOT04="; int len_str = sizeof(encoded_string) / sizeof(encoded_string[0]); // Do not count last NULL character. len_str -= 1; printf("Encoded string : %s\n", encoded_string); printf("Decoded_string : %s\n", base64Decoder(encoded_string, len_str)); return 0;} |
Java
// Java Program to decode a base64// Encoded String back to ASCII Stringclass GFG{ static final int SIZE = 100;/* char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */static String base64Decoder(char encoded[], int len_str){ char []decoded_String; decoded_String = new char[SIZE]; int i, j, k = 0; // stores the bitstream. int num = 0; // count_bits stores current // number of bits in num. int count_bits = 0; // selects 4 characters from // encoded String at a time. // find the position of each encoded // character in char_set and stores in num. for (i = 0; i < len_str; i += 4) { num = 0; count_bits = 0; for (j = 0; j < 4; j++) { // make space for 6 bits. if (encoded[i + j] != '=') { num = num << 6; count_bits += 6; } /* Finding the position of each encoded character in char_set and storing in "num", use OR '|' operator to store bits.*/ // encoded[i + j] = 'E', 'E' - 'A' = 5 // 'E' has 5th position in char_set. if (encoded[i + j] >= 'A' && encoded[i + j] <= 'Z') num = num | (encoded[i + j] - 'A'); // encoded[i + j] = 'e', 'e' - 'a' = 5, // 5 + 26 = 31, 'e' has 31st position in char_set. else if (encoded[i + j] >= 'a' && encoded[i + j] <= 'z') num = num | (encoded[i + j] - 'a' + 26); // encoded[i + j] = '8', '8' - '0' = 8 // 8 + 52 = 60, '8' has 60th position in char_set. else if (encoded[i + j] >= '0' && encoded[i + j] <= '9') num = num | (encoded[i + j] - '0' + 52); // '+' occurs in 62nd position in char_set. else if (encoded[i + j] == '+') num = num | 62; // '/' occurs in 63rd position in char_set. else if (encoded[i + j] == '/') num = num | 63; // ( str[i + j] == '=' ) remove 2 bits // to delete appended bits during encoding. else { num = num >> 2; count_bits -= 2; } } while (count_bits != 0) { count_bits -= 8; // 255 in binary is 11111111 decoded_String[k++] = (char) ((num >> count_bits) & 255); } } return String.valueOf(decoded_String);}// Driver codepublic static void main(String[] args){ char encoded_String[] = "TUVOT04=".toCharArray(); int len_str = encoded_String.length; // Do not count last null character. len_str -= 1; System.out.printf("Encoded String : %s\n", String.valueOf(encoded_String)); System.out.printf("Decoded_String : %s\n", base64Decoder(encoded_String, len_str));}}// This code is contributed by 29AjayKumar |
C#
// C# Program to decode a base64// Encoded String back to ASCII Stringusing System;class GFG{ static readonly int SIZE = 100;/* char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */static String base64Decoder(char []encoded, int len_str){ char []decoded_String; decoded_String = new char[SIZE]; int i, j, k = 0; // stores the bitstream. int num = 0; // count_bits stores current // number of bits in num. int count_bits = 0; // selects 4 characters from // encoded String at a time. // find the position of each encoded // character in char_set and stores in num. for (i = 0; i < len_str; i += 4) { num = 0; count_bits = 0; for (j = 0; j < 4; j++) { // make space for 6 bits. if (encoded[i + j] != '=') { num = num << 6; count_bits += 6; } /* Finding the position of each encoded character in char_set and storing in "num", use OR '|' operator to store bits.*/ // encoded[i + j] = 'E', 'E' - 'A' = 5 // 'E' has 5th position in char_set. if (encoded[i + j] >= 'A' && encoded[i + j] <= 'Z') num = num | (encoded[i + j] - 'A'); // encoded[i + j] = 'e', 'e' - 'a' = 5, // 5 + 26 = 31, 'e' has 31st position in char_set. else if (encoded[i + j] >= 'a' && encoded[i + j] <= 'z') num = num | (encoded[i + j] - 'a' + 26); // encoded[i + j] = '8', '8' - '0' = 8 // 8 + 52 = 60, '8' has 60th position in char_set. else if (encoded[i + j] >= '0' && encoded[i + j] <= '9') num = num | (encoded[i + j] - '0' + 52); // '+' occurs in 62nd position in char_set. else if (encoded[i + j] == '+') num = num | 62; // '/' occurs in 63rd position in char_set. else if (encoded[i + j] == '/') num = num | 63; // ( str[i + j] == '=' ) remove 2 bits // to delete appended bits during encoding. else { num = num >> 2; count_bits -= 2; } } while (count_bits != 0) { count_bits -= 8; // 255 in binary is 11111111 decoded_String[k++] = (char) ((num >> count_bits) & 255); } } return String.Join("",decoded_String);}// Driver codepublic static void Main(String[] args){ char []encoded_String = "TUVOT04=".ToCharArray(); int len_str = encoded_String.Length; // Do not count last null character. len_str -= 1; Console.Write("Encoded String : {0}\n", String.Join("",encoded_String)); Console.Write("Decoded_String : {0}\n", base64Decoder(encoded_String, len_str));}}// This code is contributed by 29AjayKumar |
Javascript
<script>// JavaScript Program to decode a base64// Encoded String back to ASCII Stringlet SIZE = 100;/* char_set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */function base64Decoder(encoded,len_str){ let decoded_String; decoded_String = new Array(SIZE); let i, j, k = 0; // stores the bitstream. let num = 0; // count_bits stores current // number of bits in num. let count_bits = 0; // selects 4 characters from // encoded String at a time. // find the position of each encoded // character in char_set and stores in num. for (i = 0; i < len_str; i += 4) { num = 0; count_bits = 0; for (j = 0; j < 4; j++) { // make space for 6 bits. if (encoded[i + j] != '=') { num = num << 6; count_bits += 6; } /* Finding the position of each encoded character in char_set and storing in "num", use OR '|' operator to store bits.*/ // encoded[i + j] = 'E', 'E' - 'A' = 5 // 'E' has 5th position in char_set. if (encoded[i + j].charCodeAt(0) >= 'A'.charCodeAt(0) && encoded[i + j].charCodeAt(0) <= 'Z'.charCodeAt(0)) num = num | (encoded[i + j].charCodeAt(0) - 'A'.charCodeAt(0)); // encoded[i + j] = 'e', 'e' - 'a' = 5, // 5 + 26 = 31, 'e' has 31st position in char_set. else if (encoded[i + j].charCodeAt(0) >= 'a'.charCodeAt(0) && encoded[i + j].charCodeAt(0) <= 'z'.charCodeAt(0)) num = num | (encoded[i + j].charCodeAt(0) - 'a'.charCodeAt(0) + 26); // encoded[i + j] = '8', '8' - '0' = 8 // 8 + 52 = 60, '8' has 60th position in char_set. else if (encoded[i + j].charCodeAt(0) >= '0'.charCodeAt(0) && encoded[i + j].charCodeAt(0) <= '9'.charCodeAt(0)) num = num | (encoded[i + j].charCodeAt(0) - '0'.charCodeAt(0) + 52); // '+' occurs in 62nd position in char_set. else if (encoded[i + j] == '+') num = num | 62; // '/' occurs in 63rd position in char_set. else if (encoded[i + j] == '/') num = num | 63; // ( str[i + j] == '=' ) remove 2 bits // to delete appended bits during encoding. else { num = num >> 2; count_bits -= 2; } } while (count_bits != 0) { count_bits -= 8; // 255 in binary is 11111111 decoded_String[k++] = String.fromCharCode ((num >> count_bits) & 255); } } return (decoded_String);}// Driver codelet encoded_String = "TUVOT04=".split(""); let len_str = encoded_String.length; // Do not count last null character. len_str -= 1; document.write("Encoded String : " + (encoded_String).join("")+"<br>"); document.write("Decoded_String : "+ base64Decoder(encoded_String, len_str).join("")+"<br>");// This code is contributed by rag2127</script> |
Output:
Encoded string : TUVO04= Decoded string : MENON
Time Complexity: O(N)
Space Complexity : O(1)
This article is contributed by Arshpreet Soodan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


.png)
