Python conversion from binary string to hexadecimal -


how can perform conversion of binary string corresponding hex value in python?

i have 0000 0100 1000 1101 , want 048d i'm using python 2.6.

welcome stackoverflow!

int given base 2 , hex:

>>> int('010110', 2) 22 >>> hex(int('010110', 2)) '0x16' >>>   >>> hex(int('0000010010001101', 2)) '0x48d' 

the doc of int:

int(x[, base]) -> integer  convert string or number integer, if possible.  floating 

point argument truncated towards 0 (this not include string representation of floating point number!) when converting string, use optional base. error supply base when converting non-string. if base zero, proper base guessed based on string content. if argument outside integer range long object returned instead.

the doc of hex:

hex(number) -> string  return hexadecimal representation of integer or long 

integer.


Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -