Skip to content

Instantly share code, notes, and snippets.

@Nurdok
Last active July 11, 2024 15:00
Show Gist options
  • Save Nurdok/4096182 to your computer and use it in GitHub Desktop.
Save Nurdok/4096182 to your computer and use it in GitHub Desktop.
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.

@redchair123
Copy link

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

@agfor
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
Copy link

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
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
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
Copy link

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
Copy link

@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
Copy link

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

@eordano
Copy link

eordano commented Apr 20, 2013

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

@havenwood
Copy link

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

@havenwood
Copy link

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

@kindall
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
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
Copy link

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
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