Skip to content

Commit 4d074a8

Browse files
committed
update readme to note hex
1 parent e554955 commit 4d074a8

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Uint8Array to/from base64
1+
# Uint8Array to/from base64 and hex
22

3-
base64 is a common way to represent arbitrary binary data as ASCII. JavaScript has Uint8Arrays to work with binary data, but no built-in mechanism to encode that data as base64, nor to take base64'd data and produce a corresponding Uint8Arrays. This is a proposal to fix that.
3+
base64 is a common way to represent arbitrary binary data as ASCII. JavaScript has Uint8Arrays to work with binary data, but no built-in mechanism to encode that data as base64, nor to take base64'd data and produce a corresponding Uint8Arrays. This is a proposal to fix that. It also adds methods for converting between hex strings and Uint8Arrays.
44

55
It is currently at Stage 1 of [the TC39 process](https://tc39.es/process-document/).
66

@@ -14,24 +14,32 @@ Initial spec text is available [here](https://tc39.github.io/proposal-arraybuffe
1414
let arr = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]);
1515
console.log(arr.toBase64());
1616
// 'SGVsbG8gV29ybGQ='
17+
console.log(arr.toHex());
18+
// '48656c6c6f20576f726c64'
1719
```
1820

1921
```js
2022
let string = 'SGVsbG8gV29ybGQ=';
2123
console.log(Uint8Array.fromBase64(string));
2224
// Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])
25+
26+
string = '48656c6c6f20576f726c64';
27+
console.log(Uint8Array.fromHex(string));
28+
// Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])
2329
```
2430

25-
This would add `Uint8Array.prototype.toBase64` and `Uint8Array.fromBase64` methods. The latter would throw if given a string which is not properly base64 encoded.
31+
This would add `Uint8Array.prototype.toBase64`/`Uint8Array.prototype.toHex` and `Uint8Array.fromBase64`/`Uint8Array.fromHex` methods. The latter pair would throw if given a string which is not properly encoded.
2632

2733
## Options
2834

29-
An options bag argument could allow specifying additional details such as the alphabet (to include at least `base64` and `base64url`), whether to generate / enforce padding, and how to handle whitespace.
35+
An options bag argument for the base64 methods could allow specifying additional details such as the alphabet (to include at least `base64` and `base64url`), whether to generate / enforce padding, and how to handle whitespace.
3036

3137
## Streaming API
3238

3339
Additional `toPartialBase64` and `fromPartialBase64` methods would allow working with chunks of base64, at the cost of more complexity. See [the playground](https://tc39.github.io/proposal-arraybuffer-base64/) linked above for examples.
3440

41+
Streaming versions of the hex APIs are not included since they are straightforward to do manually.
42+
3543
See [issue #13](https://github.com/tc39/proposal-arraybuffer-base64/issues/13) for discussion.
3644

3745
## Questions
@@ -44,7 +52,7 @@ Possibly we should have asynchronous versions for working with large data. That
4452

4553
### What other encodings should be included, if any?
4654

47-
I think hex makes sense, and no others. Hex is not yet implemented in the playground. The current plan is for new encodings to be added as seperate methods.
55+
I think base64 and hex are the only encodings which make sense, and those are currently included.
4856

4957
See issues [#7](https://github.com/tc39/proposal-arraybuffer-base64/issues/7), [#8](https://github.com/tc39/proposal-arraybuffer-base64/issues/8), and [#11](https://github.com/tc39/proposal-arraybuffer-base64/issues/11).
5058

@@ -62,6 +70,6 @@ That's also been the consensus when it's come up [previously](https://discourse.
6270

6371
Uint8Arrays can be partial views of an underlying buffer, so you can create such a view and invoke `.toBase64` on it.
6472

65-
### What if I want to decode a Base64 string into an existing Typed Array or ArrayBuffer?
73+
### What if I want to decode a Base64 or hex string into an existing Typed Array or ArrayBuffer?
6674

6775
While that is a reasonable things to want, I think it need not be included in the initial version of this API. We can add it later if demand proves high. Until then, copying slices of memory (e.g. using `target.set(chunk, offset)`) is quite fast.

playground/index-raw.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ <h3>Basic usage</h3>
7272
<pre class="language-js"><code class="language-js">
7373
let arr = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]);
7474
console.log(arr.toHex());
75-
// '48656c6c6f20576f726c64='
75+
// '48656c6c6f20576f726c64'
7676
</code></pre>
7777

7878
<p>Decoding a hex string to a Uint8Array:</p>

0 commit comments

Comments
 (0)