0

I have base64 encoded binary data(~2.5MB). The data is consisting of c double value array. In c language it comes with no cost using this array - you just read the whole data into memory and use double* address to access any element.

However, now I need to use this data in javascript. I'm looking for most efficient way to be able to read this array from js code.

ikhvjs
  • 5,316
  • 2
  • 13
  • 36
Pablo
  • 28,133
  • 34
  • 125
  • 215
  • Can you add a short sample of such base64 encoded data and what the expected output for it would have to be? – trincot Aug 16 '21 at 09:32
  • I don't have short sample unfortunately and not sure how to trim 2.5MB base64 data into short piece. In terms of bytes, each double is 8 byte signed number. Expected output would be `Array` of `Number` types or a stream class to access elements with index. – Pablo Aug 16 '21 at 09:35
  • Can't you produce a short sample from a short array of double in C? – trincot Aug 16 '21 at 09:37
  • @ikhvjs how is the C tag relevant here? How can somebody who has knowledge of C help here? – VLAZ Aug 16 '21 at 09:45
  • @VLAZ, I just think people who know both C and Javascript will help the issue. The question mentions the data type in C. – ikhvjs Aug 16 '21 at 09:47
  • Is this for browser or Node.js? – jabaa Aug 16 '21 at 09:54
  • @trincot. I have no knowledge/tools to produce it in C. – Pablo Aug 16 '21 at 10:13
  • @jabaa. Node.js – Pablo Aug 16 '21 at 10:14
  • _you just read the whole data into memory and use double* address to access any element._ This is not always true, you have to make sure endianness and the format is the same on both systems. C does not make a specific float format mandatory nor does C say anything about how systems should implement their endianness. Especially for floating points, there exist some systems with weird endianness. – 12431234123412341234123 Aug 16 '21 at 11:24

1 Answers1

1

You could use this function in Node:

function doublesFromBase64(base64) {
    let buff = Buffer.from(base64, 'base64');
    let result = [];
    for (let i = 0; i < buff.length; i += 8) {
        result.push(buff.readDoubleLE(i));
    }
    return result;
}

To test it, I produced a sample base64 encoded string from the following piece of C code, which uses a function base64_encode found in this answer:

double arr[] = {15.9234, 2.05e30, -19};
size_t output_length;
char * result = base64_encode((unsigned char *) arr, 3* sizeof(double), &output_length);
printf("%s", result);

This outputs:

uECC4sfYL0Cjw3dB6N85RgAAAAAAADPA

I passed this string to the function at the top:

function doublesFromBase64(base64) {
    let buff = Buffer.from(base64, 'base64');
    let result = [];
    for (let i = 0; i < buff.length; i += 8) {
        result.push(buff.readDoubleLE(i));
    }
    return result;
}

let result = doublesFromBase64("uECC4sfYL0Cjw3dB6N85RgAAAAAAADPA");

console.log(result);

This outputs:

[ 15.9234, 2.05e+30, -19 ]
trincot
  • 317,000
  • 35
  • 244
  • 286