In JavaScript, the btoa()
and atob()
functions can be used to encode and decode Base64-encoded strings, respectively.
The btoa()
function encodes a given string using the Base64 encoding scheme. For example, to encode the string "Hello, World!" in Base64, you could do the following:
index.tsx97 chars3 lines
The atob()
function decodes a Base64-encoded string and returns the original string. For example, to decode the Base64-encoded string "SGVsbG8sIFdvcmxkIQ==", you could do the following:
index.tsx97 chars3 lines
Note that these functions only work with strings in ASCII format. To encode or decode a string with non-ASCII characters, you can use the TextEncoder
and TextDecoder
APIs.
index.tsx456 chars10 lines
In the above example, the TextEncoder
and TextDecoder
APIs are used to convert the non-ASCII string into a format that can be encoded in Base64. The resulting Base64-encoded string is then decoded and converted back to the original non-ASCII string using the same APIs.
gistlibby LogSnag