Base64 encoding - PrismoSkills



Base64 encoding - PrismoSkills

Every language can be represented using a set of symbols. For example: english can be represented by a set of 26 alphabets along with some punctuation marks and numerical digits from 0 to 9.

If a language is made up of 'N' symbols, then the same can be represented by log2N bits.

Since each language is made up of different no of symbols, the number of bits for each language is different. For example: ASCII encoding used to represent english uses 8 bit characters. An encoding scheme used to represent Chinese may require a substantially larger number of bits since Chinese characters number in tens of thousands, though most of them are only minor graphic variants encountered in historical texts.


Also, there may be non-textual data such as an image where some bits make up textual header information while the rest of them represent the pixels of some image.


The point being made here is that there may exist different encoding schemes such that different number of bits may be required by each encoding.


To transmit such data over a media which supports textual data only, there should exists a common encoding scheme whose output is only textual.

Base-64 encoding provides a way of doing this by converting each group of 6 bits into a specific character. The character set chosen for output varies between different implementations of Base-64 but usually they adhere to the convention that the output characters should be printer friendly and they should also be a subset of most implementations.


With the above two guidelines, most Base-64 implementations chose A-Z, a-z and 0-9 as the first 62 characters. Difference is mostly in the choice of the remaining 2 characters.

Usually the last 2 characters are chosen from the following: +, -, /, ., :, ! etc.


Note 1: Since Base-64 encoding encodes 6 bits into characters using 8 bits, its output is always more than the input (usually 4/3 times the size of input). The only advantage gained by this inefficiency is that the output is textual and thus acceptable by most software systems.


Note 2: There exists other binary-to-text encoding schemes too with varying degree of efficiency. For example, the familiar hexadecimal system can also be considered as a binary-to-text encoding but its not widely used for this purpose because its much more inefficient than Base-64 (since it converts every 4 bits in input to 8-bit output symbols, the output of hexadecimal would be double the size of input).



Padding in Base-64


Base-64 converts in groups of 6 bits.

So for 8 bit encodings such as ASCII english, it converts 3 input characters into 4 output characters. If the number of characters in input is exact multiple of 3, then the number of characters in the output is an exact multiple of 4. For cases, where this is not so, 2 cases arise:

  1. Last group in the input has 1 character.

  2. Last group in the input has 2 characters.

For #1, the first 6 bits are processed as usual and the next 2 bits are padded with 4 0s to their right. This gives 2 output characters. To indicate that the input did not have 4 0s in the end, the output is padded with 2 '=' characters.


Similarly, for #2, there will be 16 input bits, out of which first 2 groups of 6 bits will be processed as normal. Next 4 bits are padded with 2 0s to their right. This gives 3 output characters. To indicate that the input did not have 2 0s in the end, the output is padded with 1 '=' character.


Thus by padding with 1 or 2 '=' characters, we always get 4 characters in the output.


Strictly speaking, the padding '=' characters are not needed since the number of input characters can always be found by calculating 3No/4 where No is the number of output characters. But still some implementations mandate padding.


Read full article from Base64 encoding - PrismoSkills


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts