tags:

views:

990

answers:

6

Are there any alternative to Base64 encoding?

I don't have any issues using Base64, just want to be aware of alternatives.

A: 

Uuencode used to be popular in mail clients, but I haven't seen it used in a while. There's also yEnc, but I don't know much about it.

McDowell
A: 

The advantage of Base64 is that it is still quite compact (three bytes become four characters). A disadvantage in some situations are some of characters it uses (+, =, /) and that it is case-sensitive.

Hex-encoding ("Base16") uses only 0-9,A-F, but then every byte is encoded as two characters.

Thilo
+1  A: 

It depends on what you're looking for. Assuming that you want something that encodes into 7-bit ASCII:

  • Quoted printable. Good for Latin text that has an occassional non-ASCII character. See the MIME specification.
  • UTF-7. A transfer encoding of Unicode.

I'm sure there are others...

Also if you are just trying to embed binary data in XML you might consider something like SOAP MTOM.

Jared Oberhaus
What is the advantage of SOAP MTOM over Base64 encoding?
Venkat
The original intent of base64 was to allow binary data to pass through mail servers that for historical reasons allowed only 7-bit content. Similarly, XML documents allow only characters of the document's specified character set, so base64 is a way to include arbitrary binary data in an XML message. You can use MTOM to communicate the binary data directly, as long as both ends understand this is what's happening.
Jared Oberhaus
+1  A: 
erickson
+1  A: 

There are myriads of encoding alternatives. You can devise any roll-your-own encoding scheme that you like. e.g. you could invent an encoding scheme in which each character is represented by the bitwise complement of the four-byte unicode index of that character.

You know, 0x00000020 (the space character) gets encoded as 0xffffffdf.

Question is, what purpose do you NEED an encoding for ?

The example scheme I mentioned is not very likely to be the best fit for your particular purpose. Other schemes, say, UTF-8 or UTF-16, might do, but depending on your situation, ancient-style code pages might do equally well from a functional viewpoint, and even better from a performance perspective.

Erwin Smout
A: 

If you just need to encode and decode a small amount of bytes without using Base64, consider this SO thread as a possible solution. I am using this to encode password bytes coming out of MessageDigest.

Just use some arbitrary radix (I used 16) and do not stick to Character.MAX_RADIX which may (or may not) change in the future.

mindas