user465001
user465001

Reputation: 846

How do you convert a buffer of hexadecimals to an array of hexadecimals?

var file = randomAccessFile('gen.mp3')

    file.read(0, 5, function(err, buffer) {
      console.log(buffer) 

    for (const value of buffer.values()) {
      console.log(value);
    }

    file.close(function() {
    console.log('file is closed')

       })
    })

The first output is:

<Buffer 49 44 33 03 00>

and the second:

73
68
51
3
0

I need an array, or some other data structure, where I can access the values of the first output individually. How would I do that? Why does the second output convert the hexadecimal to decimal?

Upvotes: 0

Views: 31

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

You can just access the buffer with [index]. https://nodejs.org/api/buffer.html#buffer_buf_index

You can print the values in hex if you want as well.

var file = randomAccessFile('gen.mp3')

file.read(0, 5, function(err, buffer) {
  console.log(buffer) 
  console.log(buffer[0]); // prints decimal
  console.log(buffer[0].toString(16)); //prints hexadecimal
  file.close(function() {
       console.log('file is closed')
  })
})

Upvotes: 1

Related Questions