5 ms·
I find buffer useful for its conversion functions to/from different encodings, e.g., `Buffer.from(data, 'hex')` or `Buffer.toString('base64')`. Is there a good
by kev946 3y ago
I find buffer useful for its conversion functions to/from different encodings, e.g., `Buffer.from(data, 'hex')` or `Buffer.toString('base64')`. Is there a good way to do this with `Uint8Array`?
- davidmurdoch 3y agoPolyfill Buffer.
- norman784 3y agoI would prefer not install any dependency if possible, I don't see a necessity of installing a polyfill just to not use the built in one because it's node specific.
- ash_gti 3y agoYou can use ‘atob’ and ‘btoa’ functions for some of that.
- denzquix 3y agoAlthough those functions operate on "binary strings", not Uint8Arrays, and there is no especially clean way that vanilla JS exposes to convert between the two that I am aware of.
- deleted 3y ago[deleted]
- esprehn 3y agoThose functions are fundamentally broken: https://developer.mozilla.org/en-US/docs/Glossary/Base64#javascript_support https://developer.mozilla.org/en-US/docs/Glossary/Base64#jav... See the whole section on converting arbitrary binary data and the complex ways to do it.
- mofle 3y agoPeople are working on bringing Base64/Hex conversion to JavaScript: https://github.com/tc39/proposal-arraybuffer-base64 https://github.com/tc39/proposal-arraybuffer-base64 I also provide a package to make the transition easier: https://github.com/sindresorhus/uint8array-extras https://github.com/sindresorhus/uint8array-extras (Feel free to copy-paste the code if you don't want another dependency)
- bsimpson 3y agoThanks for all the utility belts you've provided to the ecosystem. It's insane to me that something as simple as concatenating an array needs a library, but as I've shown upthread, Uint8Arrays are way too complicated to work with.