Skip to content

Add information about buffer.byteOffset property to Buffer.from(string) documentation #19301

@bartosz-m

Description

@bartosz-m

I'm was trying to make this example work:

var firstBuffer = Buffer.from('hello world');
var secondBuffer = Buffer.from(firstBuffer.buffer, 0, firstBuffer.length);

console.log (firstBuffer) // <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
console.log (secondBuffer) // <Buffer da 07 00 00 da 07 00 00 db 07 00>

assert(firstBuffer.buffer === secondBuffer.buffer, ".buffer property is the same");
assert (firstBuffer[0] == secondBuffer[0]) // fails

it didn't worked and I didn't know why until I found that offset property of the firstBuffer wasn't 0 as I would assume. I modify it to use correct offset and problem was solved, but I spend more than hour because there wasn't any info in official docs.

It would be good to mention in documentation that result of Buffer.from(string) can have offset different than 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.docIssues and PRs related to the documentations.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions