Your example uses base 62. Base 64 more efficient (because it's a power of 2), well tested, produces slightly smaller results.
use MIME::Base64; $num = 123; $encoded = encode_base64(pack("V", $num), ''); $encoded =~ s/==$//; print($encoded, $/); # ewAAAA (always 6 chars) $encoded .= '=='; $num = unpack("V", decode_base64($encoded)); print($num, $/); # 123
(If you only have int16s, lowercase 'v' (in both places) will result in 3 encoded bytes.)
or even
use MIME::Base64; $num = 123; $encoded = encode_base64(pack("V", $num), ''); $encoded =~ s/A+==$//; print($encoded, $/); # ew $encoded = substr($encoded.'AAAAAA', 0, 6) . '=='; $num = unpack("V", decode_base64($encoded)); print($num, $/); # 123
In reply to Re: convert numbers to a different (arbitrary) base
by ikegami
in thread convert numbers to a different (arbitrary) base
by hostyle
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |