Node: Buffer.toString(

Created on 21 Sep 2019  路  4Comments  路  Source: nodejs/node

  • Version: v12.10.0
  • Platform: Windows 10.0.18362 Build 18362
  • Subsystem: Buffer

Suppose we have the following code:

const n1 = 260114496583225n;
const n2 = 4161831945331610n;

console.log(n1);
console.log(n1.toString(16));
console.log(Buffer.from(n1.toString(16, 'hex')));
console.log(Buffer.from(n1.toString(16), 'hex').toString('base64'));

console.log('---');

console.log(n2);
console.log(n2.toString(16));
console.log(Buffer.from(n2.toString(16, 'hex')));
console.log(Buffer.from(n2.toString(16), 'hex').toString('base64'));

This yields the following output:

260114496583225n
ec92a02b7639
<Buffer 65 63 39 32 61 30 32 62 37 36 33 39>
7JKgK3Y5
---
4161831945331610n
ec92a02b7639a
<Buffer 65 63 39 32 61 30 32 62 37 36 33 39 61>
7JKgK3Y5

Note how the base64 values are the same even though the hex values and buffer contents are different.

If we add the following code:

const n3 = BigInt(`0x${Buffer.from('7JKgK3Y5', 'base64').toString('hex')}`);
const n4 = BigInt(`0x${Buffer.from('DskqArdjmg==', 'base64').toString('hex')}`);
console.log(n3, n3 === n1, n3 === n2);
console.log(n4, n4 === n2, n4 === n1);

We get the additional output:

260114496583225n true false
4161831945331610n true false

Is this expected behavior of Buffer.toString('base64')?

buffer question

Most helpful comment

Do I need to make sure that when I do Buffer.from(hexString, 'hex') that hexString is an even length and if its not, should I add a leading 0?

Yeah, that鈥檚 exactly what I would do here.

All 4 comments

Is this expected behavior of Buffer.toString('base64')?

I don鈥檛 think that鈥檚 the surprising thing here (i.e. the base64 encoding part works as expected).

What probably trips you up is that the hex representations of the BigInts lead to the same Buffer, right? I.e. that Buffer.from('ec92a02b7639', 'hex') and Buffer.from('ec92a02b7639a', 'hex') (not the extra a) lead to the same result?

I can see how that鈥檚 somewhat surprising, yes, but also, what would Buffer.from(..., 'hex') be supposed to do when the input value doesn鈥檛 have even length? It could automatically add padding but that might be just as unexpected. So, yes, I would say that this is correct behaviour, or at least not wrong.

@addaleax Is there a way to modify my example such that it gives the desired base64 value? Do I need to make sure that when I do Buffer.from(hexString, 'hex') that hexString is an even length and if its not, should I add a leading 0?
Thank you.

Do I need to make sure that when I do Buffer.from(hexString, 'hex') that hexString is an even length and if its not, should I add a leading 0?

Yeah, that鈥檚 exactly what I would do here.

Taking @addaleax's suggestion I've modified the initial example to get the expected output:

function ConvertBigIntToBase64(bigInt) {
  let hex = bigInt.toString(16);
  if (hex.length & 1) {
    hex = '0' + hex;
  }
  return Buffer.from(hex, 'hex').toString('base64');
}

const n1 = 260114496583225n;
const n2 = 4161831945331610n;

console.log(n1);
console.log(ConvertBigIntToBase64(n1));

console.log('---');

console.log(n2);
console.log(ConvertBigIntToBase64(n2));

Output:

260114496583225n
7JKgK3Y5
---
4161831945331610n
DskqArdjmg==

Thanks for your help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  路  3Comments

stevenvachon picture stevenvachon  路  3Comments

addaleax picture addaleax  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

Brekmister picture Brekmister  路  3Comments