| header pic goes here | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| log in for personalized pages! | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Tutor Section: How to translate into base64 and backReturn to base64 Javascript tutorialReturn to tutor home How does the actual translation from bytes to base64 characters occur? We must first set up a mapping of values (0 through 63) to base64 characters (A-Z, a-z, 0-9, '+', and '/'). We can do this by simply indexing an array.
Let's start with something simple, a text-to-base64 conversion. We will convert the string "Hello World!" to a base64 representation. We will start by getting the ASCII byte values for each letter.
Let's start with the first three characters.
"Hel" = SGVs "lo " = bG8g "Wor" = V29y "ld!" = bGQh The phrase "Hello World!" has been converted to "SGVsbG8gV29ybGQh". The original phrase has exactly 12 ASCII characters, and is represented by 16 base64 characters, exactly one and one third more than the original text. So what happens, you might ask, if you don't have exact sets of three bytes? What if you had remainder bytes left over? For example, what if the data you had was "Hello" (5 ASCII characters, 5 bytes)? What if it was "blue" (4 ASCII characters, 4 bytes)? In those cases, you have groups of less than three letters: "Hello" groups into "Hel" "lo", and "blue" groups into "blu" "e". To handle these cases, we throw one more readable character into our base64 character list. This character is not in the lookup table because it is only reserved for the two cases where you have one or two remainder bytes after grouping. We use the "=" character. Let's start with "Hello".
011011 = 27 000110 = 6 1111xx = what? xxxxxx = what? To resolve this problem, we fill the last two bits of 1111xx with 0's, so 111100 = 60. Our base64 characters so far are "bG8". Since we are missing one single complete base64 character, we add one of our special "=" characters to the back to signify that we are missing one byte. Our complete converted base64 string is now "bg8=". So the word "Hello" translates to "SGVSbg8=". We do the same thing for the word "blue", which is missing 2 bytes.
Note: I said before that base64 encoding is one and one third larger than the byte representation. In the cases were you are missing a byte, it is actually slightly more than this. The actual range is from exactly one and one third to one and one third plus two characters. Continue to decoding base64. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||