Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python Conversion

Python Number Conversion Chart

From To Expression
45 "45" str(data)
45 "101101" bin(data)
45 "2D" hex(data)
45 "\x00\x00\x00\x2d" struct.pack('!i', data)
"45" 45 int(data)
"45" "3435" data.encode('hex')
"101101" 45 int(data, 2)
"2D" 45 int(data, 16)
"2D" "\x2d" binascii.unhexlify(data) or data.decode('hex')
"\x00\x00\x00\x2d" 45 struct.unpack('!i', data)[0]
"\x2d" "2D" binascii.hexlify(data)
"3435" "45" data.decode('hex')

Comments are welcome here or in my original blog post regarding this table.

@Niggler

This comment has been minimized.

Copy link

Niggler commented Apr 20, 2013

oct(45) -> '055' and int('055', 8) -> 45

@agfor

This comment has been minimized.

Copy link

agfor commented Apr 20, 2013

Definitely handy.

bin and hex add 0b and 0x to the beginning of the strings -- '{:b}'.format and '{:x}'.format don't do that.

encode('hex') and decode('hex') could use an explanation, and note that hex has been removed as an encoding in Python 3.

My fork with those changes: https://gist.github.com/agfor/5426355

@chris-martin

This comment has been minimized.

Copy link

chris-martin commented Apr 20, 2013

Posted this on the blog too, but I realized I'd rather post here...

I forked this to compare it with Scala - https://gist.github.com/chris-martin/5426294 - but I'm not sure what to do with "\x00\x00\x00\x2d". What is this encoding (and why would you want it)?

@agfor

This comment has been minimized.

Copy link

agfor commented Apr 20, 2013

With regards to encode / decode hex: In Python 3.2+, you can do codecs.encode(b"45", "hex_codec").

@Caustic

This comment has been minimized.

Copy link

Caustic commented Apr 20, 2013

@chris-martin You would use it when you're dealing with binary encoded data. This gives deterministic positions of elements in structures relative to each other.

See here for more information.

Edit: To add to that, I saw on your site that you specialize in security. A clear application of binary encoding to infosec is shellcode!

@havenwood

This comment has been minimized.

Copy link

havenwood commented Apr 20, 2013

I forked this to compare with Ruby (https://gist.github.com/havenwood/5426260), but I'm not sure what the "45" to "3435" conversion is meant to be?

@chris-martin

This comment has been minimized.

Copy link

chris-martin commented Apr 20, 2013

@Caustic What I'm missing is the precisely what Python's str type really means. Do Python strings actually correspond directly to a specific representation as a bit array (rather than the more abstract notion of a unicode string that I'm used to on the JVM) - so "\x00\x00\x00\2d" doesn't strictly denote a 4-character string like I thought, but rather some 32 bits with no particular semantics attached?

@chris-martin

This comment has been minimized.

Copy link

chris-martin commented Apr 20, 2013

@havenwood "4" is ascii 0x34, "5" is ascii 0x35. I don't know why you'd ever want to do this conversion.

@eordano

This comment has been minimized.

Copy link

eordano commented Apr 20, 2013

ord('a') = 97
chr(97) = 'a'

@havenwood

This comment has been minimized.

Copy link

havenwood commented Apr 20, 2013

@chris-martin Good point, I wouldn't. Except for just this. :P

@havenwood

This comment has been minimized.

Copy link

havenwood commented Apr 20, 2013

Finished the Ruby translation, but gosh the "45" to "3435" and vice-versa are fugly.

@kindall

This comment has been minimized.

Copy link

kindall commented Apr 20, 2013

@chris-martin In Python 2.x, str is basically a byte array. In Python 3.x, str is a Unicode string. We're looking at Python 2.x here, it seems.

@zwegner

This comment has been minimized.

Copy link

zwegner commented Apr 20, 2013

@Niggler If you don't know what base it's in, you can specify a base of 0:
int('055', 0) -> 45 or int('0x55', 0) -> 85

@marcinantkiewicz

This comment has been minimized.

Copy link

marcinantkiewicz commented Apr 20, 2013

I'm not sure what to do with "\x00\x00\x00\x2d". What is this encoding (and why would you want it)?

It's a 4-byte int in the network byte order.

@plq

This comment has been minimized.

Copy link

plq commented Apr 21, 2013

@chris-martin, @havenwood to give you a real-world example for the hex encoding ("45" => "3435"): http://books.xmlschemata.org/relaxng/ch19-77143.html

this is quite inefficient, I know, but you sometimes need it for backwards compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment