How to parse string consistented by '0' and '1' (like 01001001) into base64 code in Node.js?

There was a popular game at Nov 11th among Chinese programmers. The ninth question is quite hard(sorry but that url contains MD5 and session for that I can paste it here). There was some guides writtern in Chinese with English keywords. And someone even tried to solve that with one line of Haskell...

So, the question:

given a string looks like this, and get the key to the next question:

01001000 00110100 01110011 01001001 01000001 01000011 01001010 01001011
01101110 01101100 01000001 01000001 01000001 00101011 00110001 01011010
01000010 00110001

I copyed the whole binary string into a gist here: https://gist.github.com/4054140

According to the guides, the binary code can be read as base64 code, then it can be shown as an image..

My question is: how to parse these binary string into base64 code?

These looks like numbers in base 2. If that is the case you could take the first 6 bits of each number, for example "010010" out of the first set ("01001000") and get the decimal value for these 6 bits. In the case of the first number you'll get 18 base 10:

 base10 = parseInt("010010", 2) -> 18

The next step would be to get the base64 for that decimal number. According to this table http://en.wikipedia.org/wiki/Base64 18 would be mapped to "S"

Then you take the next set of 6 bits which would be the remaining 2 from the first set of 8 ("00") plus 4 bits from the next segment ("0011") and perform the same operation:

 base10 = parseInt("000011", 2) -> 3

Again, decimal 3 maps to "D" according to the Wikipedia link.

And you'll keep going until you process all the bits.

This page has bit of information on how to convert numbers in base 2 to base 10: http://www.name-generators.com/javascript-2/how-to-make-binary-decimal-conversion-in-javascript.htm

I tried my best to parse it and got a tarball... only one step to my destination but..

My solution in LiveScript, hope it helps...

require! \fs
require! \buffer
require! \path
require! \zlib
{exec} = require \child_process

show = console.log 

fs.read-file \string.txt \utf8 (err, file) ->
  # throw err if err?
  res = file
    .replace /\_/g, '1'
    .replace /\n/g, ' '
    .split ' '
    .map -> parse-int it, 2
    .map -> String.from-char-code it
    .join ''

  b = new Buffer res, \base64

  zlib.gunzip b, (err, data) ->
    fs.write-file \c.tar data