Node: util.inspect for Uint8Array should print hex

Created on 4 Jan 2017  路  11Comments  路  Source: nodejs/node

Here's an example:

> var x = new Buffer(3); x[0] = 0; x[1] = 100; x[2] = 200; x
<Buffer 00 64 c8>
> var y = new Uint8Array(3); y[0] = 0; y[1] = 100; y[2] = 200; y
Uint8Array [ 0, 100, 200 ]

I would expect the typed array to print hex just like Buffer. FWIW technically speaking typed arrays aren't actually arrays, so the argument that it should print like Array doesn't necessarily apply:

> [x,y].map(Array.isArray)
[ false, false ]

Arguably util.inspect should print all integer typed arrays {Ui,I}nt{8,16,32}Array with hex entries

feature request util

Most helpful comment

When you console.log a typed array in a browser, it prints the elements in decimal, not hexadecimal. I don't really see a reason to deviate from that in node; it would be trading one kind of consistency for another.

All 11 comments

Semantically speaking, one will expect an Uint8Array to be printed with their contents interpreted as int since it is a TypedArray. And buffer being just a chunk of memory should be expected to print raw memory.

If we're really going down that road @fhalde you would expect a Uint8Array to be printed with their contents interpreted as Uint8, aka uint8_t in C, since it's in the name. The closest thing to an 8-bit integer in node is an entry in a Buffer, which as you probably noticed is printed as hexadecimal.

I don't understand. Uint8 is a container for 8 bit integers. a[0] = 256 becomes a[0] == 0

@fhalde I don't know if you've used those objects, but both Uint8Array and Buffer appear to act the same way:

> var x = new Uint8Array(1), y = new Buffer(1)
undefined
> x[0] = 256
256
> y[0] = 256
256
> x[0]
0 // <-- 256 "became" 0
> y[0]
0 // <-- 256 "became" 0
> x
Uint8Array [ 0 ]
> y
<Buffer 00>

Just to avoid any confusion, Buffer is a subclass of Uint8Array, so except for added/overridden methods they will always behave the same.

(By the way, one of these methods is the custom inspect function that you鈥檙e talking about here).

@addaleax thanks for the clarification! IMHO the custom inspect function for Buffer definitely should be lifted up to Uint8Array. I also think a similar output should be used for the others. It doesn't look too difficult to implement and the tests are fairly obvious, so if you think it's worth doing we could submit a PR

I don't think you should monkey-patch built-ins like Uint8Array. Adding a special case inside util.inspect itself, like is done for other builtins (bool, Error, number, null, etc.) seems OK though.

In that case all TypedArray should be treated in the same way? And to change the formatTypedArray by adding a new formatHex in place of formatNumber in utils.js, if it falls in good first contribution, I am ready to pick this one. 馃槉

It seems like a bad idea to format Float32Arrays as hex, so I don't think all typed arrays should be hex.

Similarly I am not sure about Uint32Arrays.

Hex is usually used when representing bytes, which are uint8s, so that seems like a special case to me.

Okay, so bytes can be represented in hex mode consistently throughout node APIs like it does in case of Buffer?

When you console.log a typed array in a browser, it prints the elements in decimal, not hexadecimal. I don't really see a reason to deviate from that in node; it would be trading one kind of consistency for another.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fanjunzhi picture fanjunzhi  路  3Comments

akdor1154 picture akdor1154  路  3Comments

willnwhite picture willnwhite  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

danialkhansari picture danialkhansari  路  3Comments