{@code BaseEncoding.base32().encode("foo".getBytes(Charsets.US_ASCII))}
returns the string {@code "MZXW6==="}, and
{@code byte[] decoded = BaseEncoding.base32().decode("MZXW6===");}
...returns the ASCII bytes of the string {@code "foo"}.
By default, {@code BaseEncoding}'s behavior is relatively strict and in accordance with RFC 4648. Decoding rejects characters in the wrong case, though padding is optional. To modify encoding and decoding behavior, use configuration methods to obtain a new encoding with modified behavior:
{@code BaseEncoding.base16().lowerCase().decode("deadbeef");}
Warning: BaseEncoding instances are immutable. Invoking a configuration method has no effect on the receiving instance; you must store and use the new encoding instance it returns, instead.
{@code // Do NOT do this BaseEncoding hex = BaseEncoding.base16(); hex.lowerCase(); // does nothing! return hex.decode("deadbeef"); // throws an IllegalArgumentException}
It is guaranteed that {@code encoding.decode(encoding.encode(x))} is always equal to{@code x}, but the reverse does not necessarily hold.
Encoding | Alphabet | {@code char:byte} ratio | Default padding | Comments |
---|---|---|---|---|
{@link #base16()} | 0-9 A-F | 2.00 | N/A | Traditional hexadecimal. Defaults to upper case. |
{@link #base32()} | A-Z 2-7 | 1.60 | = | Human-readable; no possibility of mixing up 0/O or 1/I. Defaults to upper case. |
{@link #base32Hex()} | 0-9 A-V | 1.60 | = | "Numerical" base 32; extended from the traditional hex alphabet. Defaults to upper case. |
{@link #base64()} | A-Z a-z 0-9 + / | 1.33 | = | |
{@link #base64Url()} | A-Z a-z 0-9 - _ | 1.33 | = | Safe to use as filenames, or to pass in URLs without escaping |
All instances of this class are immutable, so they may be stored safely as static constants. @author Louis Wasserman @since 14.0
|
|
|
|