Skip to content

Buffer.toString( #29638

@MichaelMitchellM

Description

@MichaelMitchellM
  • 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')?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions