| 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 We will now tackle translating from base64 characters back into normal bytes. We will use the same mapping of values (0 through 63) to base64 characters (A-Z, a-z, 0-9, '+', and '/').
Once again, we start by chopping it into smaller pieces and work on each piece. When we are decoding a base64 string into normal bytes, we use 4 characters at a time instead of the 3 we used when encoding. So our base64 string is broken up from "YmFzZTY0IGlzIGZ1biEh" into "YmFz", "ZTY0", "IGlz", "IGZ1", and "biEh". Lets start with our first group, "YmFz".
"IGlz" = " is" "IGZ1" = " fu" "biEh" = "n!!" The encoded base64 string "YmFzZTY0IGlzIGZ1biEh" has been decoded to "base64 is fun!!". Now we know how to encode bytes when we don't have exact groups of three to work with. But how do you decode base64 that has our special symbol, "="? It is very similar, you just have to remember the rules that caused us to use the "=". One thing before we get started: base64 encoded text will always be in groups of 4 base64 characters; if the number of base64 characters is not divisible by 4 with no remainder, then you have corrupted data. Let's try decoding a base64 string that contains the "=" symbol. Our string this time will be "Li4ub3IgbWF5YmUgbm90Lg==". The first thing we do, is divide this up into groups of four characters. "Li4ub3IgbWF5YmUgbm90Lg==" becomes "Li4u", "b3Ig", "bWF5", "YmUg", bm90", and "Lg==". The first five quartets are decoded in the exact same manner. We just need to learn what to do for the last quartet, "Lg==". Remember what the "="s mean: one "=" means that we were missing one whole byte when we encoded the data, two "="s means that we were missing two whole bytes when we encoded the data. We begin in the same way as before.
00101110 = 46 We convert the 46 to ASCII and we get the character '.' and add this to the other data that we have decoded. "Li4u" = "..." "b3Ig" = "or " "bWF5" = "may" "YmUg" = "be " "bm90" = "not" "Lg==" = "." Our final decoded string is "...or maybe not." Continue on to encode and decode your own base64. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||