Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27023 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2015-03-13 23:15 -0700 |
| Last post | 2015-03-14 09:46 -0400 |
| Articles | 7 — 3 participants |
Back to article view | Back to comp.lang.javascript
Base64 encoding jonas.thornvall@gmail.com - 2015-03-13 23:15 -0700
Re: Base64 encoding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-14 10:56 +0000
Re: Base64 encoding jonas.thornvall@gmail.com - 2015-03-14 04:12 -0700
Re: Base64 encoding jonas.thornvall@gmail.com - 2015-03-14 04:20 -0700
Re: Base64 encoding jonas.thornvall@gmail.com - 2015-03-14 05:01 -0700
Re: Base64 encoding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-14 12:45 +0000
Re: Base64 encoding Richard Damon <Richard@Damon-Family.org> - 2015-03-14 09:46 -0400
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-13 23:15 -0700 |
| Subject | Base64 encoding |
| Message-ID | <0f320550-d218-40ea-9e9b-43a01ab53b6b@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-03-14 10:56 +0000 |
| Message-ID | <87sid7u9tj.fsf@bsb.me.uk> |
| In reply to | #27023 |
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.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-14 04:12 -0700 |
| Message-ID | <5648c915-2e25-40ad-9d21-70a6c0125320@googlegroups.com> |
| In reply to | #27024 |
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?
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-14 04:20 -0700 |
| Message-ID | <374b5df7-86b8-4288-907f-ff1e22628cf1@googlegroups.com> |
| In reply to | #27025 |
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?
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-03-14 05:01 -0700 |
| Message-ID | <db80772b-a929-4fd3-958e-940a388c0b55@googlegroups.com> |
| In reply to | #27027 |
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?
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-03-14 12:45 +0000 |
| Message-ID | <87lhizu4qi.fsf@bsb.me.uk> |
| In reply to | #27030 |
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/info/rfc4648 so it's not hard to find all the details. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Richard Damon <Richard@Damon-Family.org> |
|---|---|
| Date | 2015-03-14 09:46 -0400 |
| Message-ID | <4ZWMw.37078$uF6.22209@fx17.iad> |
| In reply to | #27023 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web