[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Base64 encoding

JT

3/14/2015 6:16:00 AM

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?
6 Answers

Ben Bacarisse

3/14/2015 10:56:00 AM

0

jonas.thornvall@gmail.com writes:

> 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 main error is that the values from charCodeAt are not eight bits
wide but up to 16 bits wide, but you also need to take care of the
padding issue -- the number of encoded bytes may not be a multiple of
three. Obviously the issue is slightly different when encoding 16-bit
values.

Aside: converting between different bases that are powers of two is just
a matter of grouping the bits differently, so it can all done by shifts
and bit-wise ands and ors.

--
Ben.

JT

3/14/2015 11:12:00 AM

0

Den lördag 14 mars 2015 kl. 11:56:15 UTC+1 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > 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 main error is that the values from charCodeAt are not eight bits
> wide but up to 16 bits wide, but you also need to take care of the
> padding issue -- the number of encoded bytes may not be a multiple of
> three. Obviously the issue is slightly different when encoding 16-bit
> values.
>
> Aside: converting between different bases that are powers of two is just
> a matter of grouping the bits differently, so it can all done by shifts
> and bit-wise ands and ors.
>
> --
> Ben.

But isn't the ascii table 8-bit? Is is unicode?
But i only need 8-bit for a unique encoding?

If i treat an ASCII value as an eight bit number, and then convert it to radix 64, if it is not base 64 what is then called?

JT

3/14/2015 11:21:00 AM

0

Den lördag 14 mars 2015 kl. 12:12:14 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 14 mars 2015 kl. 11:56:15 UTC+1 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> >
> > > 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 main error is that the values from charCodeAt are not eight bits
> > wide but up to 16 bits wide, but you also need to take care of the
> > padding issue -- the number of encoded bytes may not be a multiple of
> > three. Obviously the issue is slightly different when encoding 16-bit
> > values.
> >
> > Aside: converting between different bases that are powers of two is just
> > a matter of grouping the bits differently, so it can all done by shifts
> > and bit-wise ands and ors.
> >
> > --
> > Ben.
>
> But isn't the ascii table 8-bit? Is is unicode?
> But i only need 8-bit for a unique encoding?
>
> If i treat an ASCII value as an eight bit number, and then convert it to radix 64, if it is not base 64 what is then called?

Maybe i missunderstand you are you saying that three ASCII character encoded as 16-bit? Then it is just a subset of the ASCII character set allowed?

JT

3/14/2015 12:01:00 PM

0

Den lördag 14 mars 2015 kl. 12:20:43 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 14 mars 2015 kl. 12:12:14 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 14 mars 2015 kl. 11:56:15 UTC+1 skrev Ben Bacarisse:
> > > jonas.thornvall@gmail.com writes:
> > >
> > > > 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 main error is that the values from charCodeAt are not eight bits
> > > wide but up to 16 bits wide, but you also need to take care of the
> > > padding issue -- the number of encoded bytes may not be a multiple of
> > > three. Obviously the issue is slightly different when encoding 16-bit
> > > values.
> > >
> > > Aside: converting between different bases that are powers of two is just
> > > a matter of grouping the bits differently, so it can all done by shifts
> > > and bit-wise ands and ors.
> > >
> > > --
> > > Ben.
> >
> > But isn't the ascii table 8-bit? Is is unicode?
> > But i only need 8-bit for a unique encoding?
> >
> > If i treat an ASCII value as an eight bit number, and then convert it to radix 64, if it is not base 64 what is then called?
>
> Maybe i missunderstand you are you saying that three ASCII character encoded as 16-bit? Then it is just a subset of the ASCII character set allowed?

Are they encoded as triplets?

Ben Bacarisse

3/14/2015 12:46:00 PM

0

jonas.thornvall@gmail.com writes:

> Den lördag 14 mars 2015 kl. 12:20:43 UTC+1 skrev jonas.t...@gmail.com:
>> Den lördag 14 mars 2015 kl. 12:12:14 UTC+1 skrev jonas.t...@gmail.com:
>> > Den lördag 14 mars 2015 kl. 11:56:15 UTC+1 skrev Ben Bacarisse:
>> > > jonas.thornvall@gmail.com writes:
<snip>
>> > > > Could the below logic represent how a conversion could be done.
<snip>
>> > > > 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 main error is that the values from charCodeAt are not eight bits
>> > > wide but up to 16 bits wide, but you also need to take care of the
>> > > padding issue -- the number of encoded bytes may not be a multiple of
>> > > three. Obviously the issue is slightly different when encoding 16-bit
>> > > values.
<snip>
>> > But isn't the ascii table 8-bit?

Yes, but ECMAScript uses 16 bit numbers to represent characters in a
string. You are free to limit yourself to only encoding those strings
that containing character values from 0 to 255, but there will be
strings with characters outside of this range.

<snip>
>> Maybe i missunderstand you are you saying that three ASCII character
>> encoded as 16-bit? Then it is just a subset of the ASCII character
>> set allowed?
>
> Are they encoded as triplets?

In a sense, yes. Think about how the two strings "a" and "aa" would be
base-64 encoded. The encoding is specified by an RFC:

https://www.rfc-editor.org/in...

so it's not hard to find all the details.

--
Ben.

Richard Damon

3/14/2015 1:47:00 PM

0

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.