0

Here is my problem:

I have a base64 encoded string, a very long one (344 characters).
When I decode this string I can obtain an array of bytes.

Let's say my base64 encoded string is ALVuSLbT.
Decoded it gives me the following array: [0, 181, 110, 72, 182, 211].

And I need to obtain this number: 779239339731 (the original encoded value).
I know how to do this manually.
000000000000000000000000000000000000000000000000 000000001011010100000000000000000000000000000000 000000000000000001101110000000000000000000000000 000000000000000000000000010010000000000000000000 000000000000000000000000000000001011011000000000 000000000000000000000000000000000000000011010011

With this simple example I could easily use parseInt(binaryString, 2) and add numbers together. But my original string is too big for this.

And I want to get a big number that can convert this number to a string ('779239339731').
I didn't find a way to do this or a BigInteger/BigNumber javascript library that allows me to pass an array of bytes to create the BigInteger object.

You can check this jsfiddle.

Can someone help me with this? Is there a way to handle such a thing in Javascript?

1 Answer 1

1

I don't get why bother parsing - using the BigInteger library https://rawgithub.com/silentmatt/javascript-biginteger/master/biginteger.js

Working jsFiddle example - http://jsfiddle.net/svejdo1/sdV8L/

var exponentB64 = 'ALVuSLbT';
var exponentBytes = base64_decode(exponentB64);
var result = new BigInteger(0);
var multiplier = new BigInteger(1);
for(var i = exponentBytes.length - 1; i >= 0; i--) {
    result = result.add(multiplier.multiply(exponentBytes[i]));
    multiplier = multiplier.multiply(256);
}
document.write(result.toString());


function base64_decode(base64String) {
  var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  var h1, h2, h3, h4, o1, o2, o3, bits, i = 0, bytes = [];

  do {
    h1 = b64.indexOf(base64String.charAt(i++));
    h2 = b64.indexOf(base64String.charAt(i++));
    h3 = b64.indexOf(base64String.charAt(i++));
    h4 = b64.indexOf(base64String.charAt(i++));

    bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

    o1 = bits >> 16 & 0xff;
    o2 = bits >> 8 & 0xff;
    o3 = bits & 0xff;

    bytes.push(o1);
    bytes.push(o2);
    bytes.push(o3);
  } while (i < base64String.length);

  return bytes;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh it's that easy? Let's use some real world example now: encoded string 'AK2/ss9tBm5mc4wThXztiHh5ApZDjkYA4//8vUtYtsCD6sUQ/GIoNwy7qIm4YIrr68c479nWXmIyhDVMuHZk4/Bque4xdPkNYMz5agOTweVa+NWuvWIivAZ34rHCzOVtM/Klkg/g7d0dIzYOkPfQmweyx1Bmdh2HVaKwmngvzb70d0cr7LN3YL6aDeg++w0d99INxEWMDb8s0VPVx5BHC0iOTS34JvB4vicxm2yfxzr6Y4cOJDfp1zoReogixp+zxljcMQ16dJly52tqBQH1lT8NWEtiAw8karYMRCBNKl9dn9d7FhopaAsJgZAHmZBuD6hi6IUZ6EHsUNm07xyU8S8=' expected decoded number 219337568015997254830809010245695848800620186109619871851429669559883715518752323465558437623481096892170... "Your message is too long by 461 characters..."
I'm sorry - but this is your fault; your string contains invisible empty unicode characters 8204 and 8203; you said the string is base64, but it is not. Also down voting answers, which are providing correct output for given data - way to go (I even tested in C# using Microsoft System.Numeric.BigInteger with the same results).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.