Skip to content

Recommended approach to generate URL valid base64 strings. #93

Answered by 101arrowz
axyz asked this question in Q&A
Discussion options

You must be logged in to vote

You're actually already using the most effective strategy possible for what you're trying to do. If you log the compressed and base64 encoded lengths, you'll see that btoa(strFromU8(compressedData, true)) is always 33% larger than compressedData, which is the optimal result for Base64 encoding. You're correct that btoa and atob don't support Unicode: they only support Latin-1, a 256 character subset. Regardless, if you try to do strToU8(strFromU8(compressedData)) it will actually not work because compressedData is not valid UTF-8 in the first place.

The one change you can make is how you encode the original string. Right now you're assuming your input is only Latin-1, i.e. adding emojis w…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@axyz
Comment options

Answer selected by axyz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants