Converting between Octal, Binary, Hex

  • ord
  • hex
  • chr
  • int
  • bin
  • format

Lets convert a string of characters into its unicode value for each character using list comprehension with ord and join method


In [28]:
s1 = 'This is a test string'
# [ord(x) for x in s1] This list comprehension gives us a list of integer values for each character
# join takes a sequence of str() and joins them
' '.join([str(ord(x)) for x in s1])


Out[28]:
'84 104 105 115 32 105 115 32 97 32 116 101 115 116 32 115 116 114 105 110 103'

Lets convert a string of characters into its unicode value for each character using format and join method


In [45]:
' '.join([format(ord(x)) for x in s1])


Out[45]:
'84 104 105 115 32 105 115 32 97 32 116 101 115 116 32 115 116 114 105 110 103'

What about a text string into binary


In [49]:
' '.join([format(ord(x), 'b') for x in s1])


Out[49]:
'1010100 1101000 1101001 1110011 100000 1101001 1110011 100000 1100001 100000 1110100 1100101 1110011 1110100 100000 1110011 1110100 1110010 1101001 1101110 1100111'

If we want it to be 8 bits long


In [50]:
' '.join([format(ord(x), 'b').zfill(8) for x in s1])


Out[50]:
'01010100 01101000 01101001 01110011 00100000 01101001 01110011 00100000 01100001 00100000 01110100 01100101 01110011 01110100 00100000 01110011 01110100 01110010 01101001 01101110 01100111'

In [ ]:

What about a text string into hex using format and join

converting each character into its unicode ordinal value then format that result into a hex while joining each character into a string thats space seperated each value


In [48]:
' '.join([format(ord(x), 'x') for x in s1])


Out[48]:
'54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 73 74 72 69 6e 67'

What about a text string into octal


In [51]:
' '.join([format(ord(x), 'o') for x in s1])


Out[51]:
'124 150 151 163 40 151 163 40 141 40 164 145 163 164 40 163 164 162 151 156 147'

In [ ]:

ord()

Takes a single character and returns its unicode value


In [26]:
ord?

In [27]:
# This is an integer value
ord('T')


Out[27]:
84

hex()

Takes an integer and returns hexadecimal representation


In [29]:
hex(16)


Out[29]:
'0x10'

In [ ]:

chr()

returns a unicode string of one character with ordinal i


In [30]:
chr?

In [33]:
chr(84)


Out[33]:
'T'

bin()

Takes an int and returns the binary representation


In [1]:
bin?

int

takes a number or string representing a number and a base and converts into an integer When specifying a base, the first argument must be a string

With int you specify the base of the string, not the base your trying to convert to converts everything to ints


In [38]:
int('0xDEADBEEF', 16)


Out[38]:
3735928559

In [39]:
int('DEADBEEF', 16)


Out[39]:
3735928559

In [40]:
int('DEADBEEF')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-cec20055003e> in <module>()
----> 1 int('DEADBEEF')

ValueError: invalid literal for int() with base 10: 'DEADBEEF'

In [41]:
int('0oDEADBEEF', 16)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-14764782b3fc> in <module>()
----> 1 int('0oDEADBEEF', 16)

ValueError: invalid literal for int() with base 16: '0oDEADBEEF'

Given a hex/octal/binary number, how do you get the ascii/utf-8 string back

  • convert everything back to an int using int() and specifying the base
  • convert ints back into ordinal using chr()

In [55]:
chr(int('54', 16))


Out[55]:
'T'

In [56]:
chr(int('124', 8))


Out[56]:
'T'

In [57]:
chr(int('01010100', 2))


Out[57]:
'T'

format

Specify how to format the output with base and size


In [32]:
format?

In [34]:
format(x, 'x')


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-34-728353f98d04> in <module>()
----> 1 format('T', 'x')

ValueError: Unknown format code 'x' for object of type 'str'

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: