The io_lib:fread function takes an optional radix (which must be
an exact integer, from 2 to 36). If not supplied, it defaults to decimal.
You can provide specific formatting for numbers using the
io_lib:format or io:fwrite functions (which use the same syntax):
4> io:fwrite("~.16B\n", [123]).
7B
Note that the radix and numbers may be upper or lower case.
Here's an example that accepts a number in either decimal, octal, or hex, and prints that number in all three bases. It uses the oct function to convert from octal and hexadecimal if the input began with a 0. It then uses printf to convert back into hex, octal, and decimal as needed.
The following code converts Unix file permissions. They're always given in octal, so we assume base 8:
fp_convert(Str) ->
{ok, [Num], _ } = io_lib:fread("~8u", Str),
io:fwrite("The decimal value is ~B~n", [Num]).
6> cookbook:fp_convert("777").
The decimal value is 511
ok