math.bases
integers in hex, binary, octal or any base from 2 to 62, custom digits, spreadsheet column letters and phone keypad digits
Integers written in other bases: binary, octal, hex, or any base from 2 to 36.
writing
Section titled “writing”Type a number in another base with a prefix: 0b1011 for binary, 0o17 for octal, 0xff for hex, or 36#zz for any base up to 36. A number remembers the base it was written in, so 0xff + 1 shows as 0x100.
good to know
Section titled “good to know”Computers store negative numbers in two’s complement, where with a fixed number of bits -1 is all ones. Give that number of bits, as in -1 to hex(16), to see a negative number that way.
Tags: conversion
hexan integer in hex, the same asv to hexbinan integer in binary, the same asv to binoctan integer in octal, the same asv to octdecan integer in decimal, the same asv to decbasean integer in any base from 2 to 62, the same asv to base(b)to_basean integer as text in a base from 2 to 62, or in your own digits:to_base(10, "ab")is “baba”from_basethe integer written in text in a base from 2 to 62, or in your own digits, the reverse of to_basephone_digitsthe digits that a phone keypad gives letters, so “FLOWERS” is “3569377”base_tablea one-row table of an integer in binary, octal, decimal and hexcolumn_namethe spreadsheet column letters for column n, counting from 1column_numberthe column number, counting from 1, of spreadsheet column lettersdigitsthe list of a number’s digits, in the base it was written infrom_digitsput digits back together into a number, the reverse of digits
syntax
Section titled “syntax”1# to hex, to bin, to oct or to dec2255 to bin0b111111113# to hex(bits): negatives in that many bits4-1 to hex(16)0xffff5# to base(b): any base from 2 to 626255 to base(36)36#737# to bases: binary, octal, decimal and hex side by side8255 to basestable([{bin: 0b11111111, oct: 0o377, dec: 255, hex: 0xff}])Functions
Section titled “Functions”hex(v: int|float|str|bytes, bits?: int) -> int|str
an integer in hex, the same as v to hex
With bits, a negative number in two’s complement. A string is read as hex digits, with or without 0x, and bytes become the hex of each byte
1hex(255)0xff2hex(-1, 16)0xffff3hex("0x65")0x654hex(b"hi")"6869"See also bin, base, int, decode, encode
bin(v: int|float, bits?: int) -> int
an integer in binary, the same as v to bin
With bits, padded to that many bits and negatives in two’s complement
1bin(10)0b10102bin(5, 8)0b00000101oct(v: int|float, bits?: int) -> int
an integer in octal, the same as v to oct
1oct(8)0o10dec(v: int|float, bits?: int) -> int
an integer in decimal, the same as v to dec
With bits, a negative number becomes the positive one with the same bits, so -1 in 8 bits is 255
1dec(0xff)2552dec(-1, 8)255base(v: int|float, b: int) -> int|str
an integer in any base from 2 to 62, the same as v to base(b)
Digits past 9 are lowercase letters, then capitals past base 36. Past base 36 the result is text, since only bases up to 36 are numbers
1base(35, 36)36#z2base(10, 3)3#1013base(61, 62)"Z"to_base
Section titled “to_base”to_base(n: int, digits: int|str) -> str
an integer as text in a base from 2 to 62, or in your own digits: to_base(10, "ab") is “baba”
A custom alphabet needs at least 2 distinct characters, and its length is the base
1to_base(255, 62)"47"2to_base(10, "ab")"baba"from_base
Section titled “from_base”from_base(s: str, digits: int|str) -> int
the integer written in text in a base from 2 to 62, or in your own digits, the reverse of to_base
1from_base("zz", 36)12952from_base("baba", "ab")10See also to_base
phone_digits
Section titled “phone_digits”phone_digits(s: str) -> str
the digits that a phone keypad gives letters, so “FLOWERS” is “3569377”
Anything that isn’t a letter stays as it is
1phone_digits("FLOWERS")"3569377"2phone_digits("1-800-GOT-JUNK")"1-800-468-5865"See also from_base
base_table
Section titled “base_table”base_table(v: int|float) -> table
a one-row table of an integer in binary, octal, decimal and hex
1255 to basestable([{bin: 0b11111111, oct: 0o377, dec: 255, hex: 0xff}])2base_table(10)table([{bin: 0b1010, oct: 0o12, dec: 10, hex: 0xa}])column_name
Section titled “column_name”column_name(n: int) -> str
the spreadsheet column letters for column n, counting from 1
1column_name(28)"AB"2column_name(703)"AAA"See also column_number
column_number
Section titled “column_number”column_number(s: str) -> int
the column number, counting from 1, of spreadsheet column letters
1column_number("AB")28See also column_name
digits
Section titled “digits”digits(n: int) -> list
the list of a number’s digits, in the base it was written in
11234.digits[1, 2, 3, 4]20b1011.digits[1, 0, 1, 1]See also from_digits
from_digits
Section titled “from_digits”from_digits(xs: list, base: int = 10) -> int
put digits back together into a number, the reverse of digits
The result shows in the base the digits are in
1[1, 2, 3].from_digits1232[1, 0, 1, 1].from_digits(2)0b1011See also digits
More examples
Section titled “More examples”1# 255 in binary2255 to bin0b111111113# a base-36 number in decimal436#zz to dec12955# hex of a big int6factorial(20) to hex0x21c3677c82b400007# results keep the base they were written in80xff + 10x1009# -1 in 16-bit two's complement10-1 to hex(16)0xffff11# base 3612255 to base(36)36#7313# the sum of the digits of 2 to the 100th14(2 ** 100).digits.sum115Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.