Convert an Uint8Array to a base64 encoded string
Encode byte values as base64 with btoa.
function bytesToBase64(bytes) { return btoa(String.fromCharCode(...bytes)); }
Input & expected output
Input
[
72,
101,
108,
108,
111
]Expected output
"SGVsbG8="The edge case
Input
[]Expected output
""Know the limits
- Spreading is fine for the lab’s 1,000 bytes. For files, use Buffer.from(bytes).toString("base64") in Node.js or encode in chunks.
- This is the encoding Oveyon expects in an attachment’s content field.
The function above is the implementation used by the lab. The lab validates JSON types and caps size before execution; include equivalent validation when accepting untrusted input in your application.
Source & maintenance
Based on Convert an uint8 array to a base64 encoded string from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Uses String.fromCharCode with spread instead of building an array of characters; bytes validated 0–255. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Capitalize a string — Uppercase the first Unicode code point and preserve the remaining text.
- Reverse a string — Reverse Unicode code points without splitting surrogate pairs.
- Check a palindrome — Compare a string with its code-point reversal.