1

In JavaScript on Firefox, a Uint8Array shows like this on the console, using console.log:

Uint8Array […]
0: 131
1: 165
2: 116
3: 111
4: 112

How can I make it show as hexadecimal values, like this:

Uint8Array […]
0: 0x83
1: 0xa5
2: 0x74
3: 0x6f
4: 0x70

I'm trying to debug some websocket communication and have to look up the bytes in the format specification which only lists the values in hex.

1 Answer 1

5

You can't make the console do that (at least I know of no way to do that), but you could transform the array explicitly:

console.log([].map.call(yourArray, x => x.toString(16))

edit — thanks for the correction; the typed arrays return new typed arrays from their .map().

You could also make it a regular array with Array.from().

Sign up to request clarification or add additional context in comments.

3 Comments

Using this, all values that have letters in their hex value are converted to 0. And the type remains a Uint8Array, not some string array. Trying to find the bug.
Doesn't work with TypedArray because their map creates another typed array.
@ygoe right that or just using the "normal" map version from plain arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.