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

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:

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

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

``````
``````

Out:

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

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

``````
``````

Out:

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

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

``````
``````

Out:

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

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

``````
``````

Out:

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

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

``````
``````

Out:

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

ord?

``````
``````

In :

# This is an integer value
ord('T')

``````
``````

Out:

84

``````

## hex()

Takes an integer and returns hexadecimal representation

``````

In :

hex(16)

``````
``````

Out:

'0x10'

``````
``````

In [ ]:

``````

## chr()

returns a unicode string of one character with ordinal i

``````

In :

chr?

``````
``````

In :

chr(84)

``````
``````

Out:

'T'

``````

## bin()

Takes an `int` and returns the binary representation

``````

In :

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 :

``````
``````

Out:

3735928559

``````
``````

In :

``````
``````

Out:

3735928559

``````
``````

In :

``````
``````

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

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

``````
``````

In :

``````
``````

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-41-14764782b3fc> in <module>()

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 :

chr(int('54', 16))

``````
``````

Out:

'T'

``````
``````

In :

chr(int('124', 8))

``````
``````

Out:

'T'

``````
``````

In :

chr(int('01010100', 2))

``````
``````

Out:

'T'

``````

## format

Specify how to format the output with base and size

``````

In :

format?

``````
``````

In :

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 [ ]:

``````