**Decimal, Binary and Hexadecimal in Python**

Converting from Binary to Hexadecimal

How do we go about this?

**The Basics**

Hexadecimal

What is Denary?

What is Binary?

We know that Binary is:

A base 2 number system

With 1s and 0s

We can calculate a binary number by using a basic table:

What is Hexadecimal?

We know that:

Denary is a Base 10 System, meaning it has 10 possible integers to make it.

These are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

We use this in basic written mathematics

A Base 16 number system

It uses letters as well as numbers

It is used to simply large binary bits.

Setup...

Method of Conversion

Binary to Hex

Amount of Bits

Checking Each Group of 4

List of Hexadecimal Values

You have finished

If you didn't understand any of the steps then we are going to go through the program now...

4 binary bits are 1 hexadecimal bit

We need to find a way of separating the binary value to groups of 4

Also create a dictionary of hex values greater than or equal to 10.

Adding Zeros

If there are any sets of binary values without 4 bits, we add on the extra zeros at the front.

Read each group of 4 and convert it to a decimal number.

If the value is larger than or equal to 10 we check the corresponding letter (from the dictionary).

We add each hex value to a list

Join the list and print it

To convert it you need to group each binary number into four so for example the number 1000100111101 will be

[0,0,0,1] [0,0,0,1] [0,0,1,1] [1,1,0,1]

After that we calculate each decimal value for each four bit binary value and put them together in a list.

[1,1,3,13]

Any number 10 or over will be represented by its equivalent letter so in this case:

13 = D

The final Hexadecimal value is 113D

Step 1 - Initial Declaration

Accept a decimal input so the code will look something like:

d = int(input("Denary number: "))

Remember the int() function converts a value into an integer

Set a variable called 'i' to one, we will explain that later.

Create a list called binary with the value one which will look something like this:

binary = [1,]

Create a list called con like this

con = [ ]

Step 2 - The Headings

Write a for loop (we will explain what that is) to loop through from 0 to the input value like this:

for x in range(0,d):

The 'i' you declared in the last step will be used now. Indent your for loop and write i = i * 2.

Add 'i' to the binary list by using .append()

binary.append(i)

Write an if statement checking whether i == d and also whether i >= d

if i == d:

binary.append(i)

if i >= d:

binary.remove(i)

break

Then finally reverse the list by using the function .reverse() so it looks like:

binary.reverse()

Step 3

Iteration through binary headings

Write a for loop that iterates through the Binary headings in the binary list.

for num in binary:

Write an if statement checking if the input >= num and underneath write:

input = input - num

con.append('1')

Another if statement to check if the input is less that num

elif d < num:

con.append('0')

And also if d == num:

con.append('0')

Step 4

Joining CON (The list with the final binary value)

To join con, type in underneath everything, with no indentation,

fin = "".join(con)

Final Step

Print!

End by printing the joined list (fin) The final code should look extremely similar if not identical to this:

d = int(input("Denary number: "))

i = 1

binary = [1,]

con = []

for x in range(0,d):

i = i * 2

binary.append(i)

if i == d:

binary.append(i)

if i >= d:

binary.remove(i)

break

binary.reverse()

for num in binary:

if d >= num:

d = d - num

con.append("1")

elif d < num:

con.append("0")

elif d == 0:

con.append("0")

fin = "".join(con)

print("Binary number: %s" % (fin))

Decimal to Binary

d = int(input("Denary number: "))

i = 1

binary = [1,]

con = []

for x in range(0,d):

i = i * 2

binary.append(i)

if i == d:

binary.append(i)

if i >= d:

binary.remove(i)

break

binary.reverse()

Your code so far should look similar to this...

Your code should now look like this..

d = int(input("Denary number: "))

i = 1

binary = [1,]

con = []

for x in range(0,d):

i = i * 2

binary.append(i)

if i == d:

binary.append(i)

if i >= d:

binary.remove(i)

break

binary.reverse()

for num in binary:

if d >= num:

d = d - num

con.append("1")

elif d < num:

con.append("0")

elif d == 0:

con.append("0")

Our task is going to be to make a Decimal to Binary Converter in Python!

Although you might not understand some of the steps, this will help to

THINK LIKE A PROGRAMMER!