Skip to content

Instantly share code, notes, and snippets.

@haoliplus
Created April 6, 2016 14:58
Show Gist options
  • Save haoliplus/da2b600a74fc2babcc158e46e605e76c to your computer and use it in GitHub Desktop.
Save haoliplus/da2b600a74fc2babcc158e46e605e76c to your computer and use it in GitHub Desktop.
hex, bin, oct, dec convert to each other
a = 0b101 # a = 5
a = 0xf # a = 15
a = 010 # a = 9
# to dec
int('0xf', 16) # 15
int('1011', 2) # 11
int('17', 8) # 15
# to hex
hex(1033) # '0x409'
hex(int('101010', 2)) # 0x2a
hex(int('17', 8)) # 0xf
# to bin
bin(10) #'0b1010'
bin(int('ff', 16)) # '0b11111111'
bin(int('17', 8)) # '0b1111'
# to oct
oct(0b1010) # '012'
oct(11) # '013'
oct(0xf) # '017'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment