3

I got an array which is 32 bit, big endian unsigned integers, basically in plain text is like this [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

That 32bit endian array itself is encoded in binary base64 AAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEA==

Initially, if I receive this from heaven, now how can I decode/unpack everything to that understandable plain text representation in JavaScript.

In ruby I can simply use Base64.decode and String#Unpack

encoded_string = 'AAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEA=='

decoded_string = Base64.strict_decode64(encoded_string)
  => "\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\a\x00\x00\x00\b\x00\x00\x00\t\x00\x00\x00\n\x00\x00\x00\v\x00\x00\x00\f\x00\x00\x00\r\x00\x00\x00\x0E\x00\x00\x00\x0F\x00\x00\x00\x10"

decoded_string.unpack('N*') #32-bit unsigned, network (big-endian) byte order
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]

Is there an equivalent of this simple step in JavaScript?

Mo Asghari
  • 251
  • 2
  • 11
arjun
  • 1,594
  • 16
  • 33
  • You can take a look at this: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding It should be the tools you are looking for. – Bellian Dec 04 '17 at 16:16
  • Why does this have a [ruby] tag? – Mark Thomas Dec 04 '17 at 18:08
  • It's fine to add Ruby code for reference, but only add a `[ruby]` tag if you are seeking help from Rubyists. – Stefan Nov 10 '21 at 07:46

3 Answers3

1

You could decode the given string and take onyl four characters for convering to a 32 bit number by using ArrayBuffer and DataView

function getInt(string) {
    var view = new DataView(new ArrayBuffer(4)),
        i;

    for (i = 0; i < string.length; i++) {
        view.setUint8(i, string.charCodeAt(i));
    }
    return view.getInt32(0);
}

var encodedData = 'AAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEA==',
    decodedData = window.atob(encodedData),
    result = [],
    i = 0;

while (i < decodedData.length) {
    result.push(getInt(decodedData.slice(i, i += 4)));
}

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Okay after some rigorous search I found this library specific to such purpose - https://www.npmjs.com/package/binary

Also provides other encodings

encoded_string = 'AAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEA=='  
let buffer = new Buffer(encoded_string, 'base64');
let arrayList = [];
let binaryList = binary.parse(buffer);
for (var i = 0; i <(buffer.length / 4); i++) {
    arrayList.push(binaryList.word32bu('a').vars.a);
};
arjun
  • 1,594
  • 16
  • 33
0

First window.atob('AAAAAQA') => '\x00\x00\x00\x01\x02\x03' */

const encoder = new TextEncoder()
const view = encoder.encode('\x00\x00\x00\x01\x02\x03')
console.log(view);
/* Uint8Array(6) [0, 0, 0, 1, 2, 3] */

This should be the right way!!

55utah
  • 1