Richard Damon
3/14/2015 1:47:00 PM
On 3/14/15 2:15 AM, jonas.thornvall@gmail.com wrote:
> There is alot of javascript doing base64 encoding and i have probably implemented my own, but not recently.
>
> Could the below logic represent how a conversion could be done.
> I do realise it may not be the standard way and more highlevel, require at least bignumb addition, but would it yield correct encoding?
>
> 1. Get the byte values with charCodeAt
> 2. Treat the bytevalues from string as base 256.
> 3. Convert the base 256 value to base 10. (do not know howto go from 256 to 64 right out of head)
> 4. Convert base 10 into base 64.
> 5. Map the characters upon the base 64 encoded.
>
> Maybe it is wrong?
>
The first issue is that charCodeAt doesn't give you "bytes" but Unicode
characters, which will mess up your data. WARNING many byte sequences
will generate an error if you try to interpret them as a Unicode string.
The basic Base64 encoding is to take three 8 bits bytes (24 bits) and to
express them as 4 base64 characters (6 value bits) again 24 bits. The
breaking up is best done with simple mask, shift, or operations.
Yes, if you built a 24 bit number from 3 8 bit bytes as base 256 digits,
you could convert to base 10 and then to base 64, but that seems a lot
of waste.