Storing binary data in UTF-8 string

Viewed 5815

I want to use a WebSocket to transfer binary data, but you can only use WebSockets to transfer UTF-8 strings.

Encoding it using base64 is one option, but my understanding is that base64 is most desirable when your text might be converted from one format to another. In this case, I know the data will always be UTF-8, so is there a better way of encoding binary data in a UTF-8 string without paying base64's 33% size premium?

This question is mostly academic, as binary support will probably be added to WebSocket eventually, and base64 is a perfectly cromulent alternative in the meantime.

3 Answers
Related