Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27024

Re: Base64 encoding

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.javascript
Subject Re: Base64 encoding
Date 2015-03-14 10:56 +0000
Organization A noiseless patient Spider
Message-ID <87sid7u9tj.fsf@bsb.me.uk> (permalink)
References <0f320550-d218-40ea-9e9b-43a01ab53b6b@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web