If you have binary data to encode, what encoding scheme do you use?
I know about:
- Hex encoding . Very simple, but rather verbose, extends one byte to two.
- Base 64 . The most common, not so verbose, extends three bytes to four.
- Base 85 . Not general, less verbose, extends four bytes to five.
Are there other general coding schemes? If so, what are the advantages and disadvantages?
Edit : This is useful, for example, when trying to save arbitrary data in a cookie. Cookies can only store text, not arbitrary data, so you need to somehow convert it, preferably with the ability to convert it. Also, suppose you are using a stateless server so that you cannot save state on the server and just put the identifier in a cookie. Of course, if you do this, you will also need some way to verify that what the user is transmitting to you is what you have transmitted to the user, such as a signature.
Also, since the current consensus is that you should use base64 as it is widespread, I will also point out that this is what I use ... I am just wondering if someone used something else, and if so, why.
Edit : just in case someone trips over this, if you want to use Base64 to store data in a cookie, you need to use the modified Base64 implementation . See this answer for this reason.
encoding base64 hex
Paul wagland
source share