views:

56

answers:

1

I'm trying to base64 encode a utf8 string containing Thai characters. I'm using the browser's built in btoa function. It works for ascii text, however Thai is causing it to throw a INVALID_CHARACTER_ERR: DOM Exception 5 exception.

Here's a sample that fails (the character that looks like an "n" is Thai)

btoa('aก')


What do I need to do to base64 encode non-ascii strings?

A: 

Unfortunately btoa/atob aren't specified in any standard, but the implementations in firefox and webkit both fail on multibyte characters so even if they were now specified those builtin functions would not be able to support multibyte characters (as the input and output strings would necessarily change).

It would seem your only option would be to roll your own base64 encode+decode routines

olliej