# Help with Computer Science homework?

Watch
Announcements

Page 1 of 1

Go to first unread

Skip to page:

This homework is due on Tuesday (27/03/18) and I don't understand it. Help?

The question is: "Write a sub-program that takes a 2-bit hexadecimal number as a parameter and returns the corresponding decimal number" There's a note that says "The sub-program should examine both digits of the hexadecimal number and convert any letters (A-F) to decimal numbers."

The question is: "Write a sub-program that takes a 2-bit hexadecimal number as a parameter and returns the corresponding decimal number" There's a note that says "The sub-program should examine both digits of the hexadecimal number and convert any letters (A-F) to decimal numbers."

1

reply

Report

#2

(Original post by

This homework is due on Tuesday (27/03/18) and I don't understand it. Help?

The question is: "Write a sub-program that takes a 2-bit hexadecimal number as a parameter and returns the corresponding decimal number" There's a note that says "The sub-program should examine both digits of the hexadecimal number and convert any letters (A-F) to decimal numbers."

**xxJiminiePaboxx**)This homework is due on Tuesday (27/03/18) and I don't understand it. Help?

The question is: "Write a sub-program that takes a 2-bit hexadecimal number as a parameter and returns the corresponding decimal number" There's a note that says "The sub-program should examine both digits of the hexadecimal number and convert any letters (A-F) to decimal numbers."

**| Hex | Decimal | Binary |**

-------------------------------------------

| 1 | 1 | 0001 |

So you just have to figure out a program that will take any 2 bit Hex number and covert it to decimal.

0

reply

(Original post by

A hexadecimal number will have a decimal and binary equivalent. For example, for the Hexadecimal number 1:

-------------------------------------------

| 1 | 1 | 0001 |

So you just have to figure out a program that will take any 2 bit Hex number and covert it to decimal.

**jestersnow**)A hexadecimal number will have a decimal and binary equivalent. For example, for the Hexadecimal number 1:

**| Hex | Decimal | Binary |**-------------------------------------------

| 1 | 1 | 0001 |

So you just have to figure out a program that will take any 2 bit Hex number and covert it to decimal.

0

reply

Report

#4

(Original post by

Thanks for explaining it. I'm just still stumped on writing the program. :/

**xxJiminiePaboxx**)Thanks for explaining it. I'm just still stumped on writing the program. :/

0

reply

Report

#5

Consider the way you would do this for a decimal number. For example, the number 46923 breaks down like this:

Now all you need to do is replicate this for Hexadecimal instead. Remember that there are 16 possible values in a Hexadecimal digit ranging from 0-F (or 0-15):

Also remember how the multipliers of digits work in decimal, which is also-known-as Base-10 because there are 10 possible values from 0-9

Hexadecimal is also-known-as Base-16, because there are 16 possible values from 0-F, So Hexadecimal multipliers work like this:

So in summary, it's a simple matter of isolating each digit, based on its relative position from the right-hand-side, then applying the multiplier for its position.

As far as writing code, consider the steps carefully for your Hex-to-Decimal algorithm. You cannot write code for an algorithm if you don't already understand the logical steps you need for that algorithm to work, so write those steps down using simplified plain-english (psuedocode) on paper to make sure you understand the sequence.

As far as writing code is concerned, If any problem or part of the problem seems too difficult, then break it down into a smaller problem, and if necessary, try entering the questions for your problems into Google or StackOverflow.

Most programming problems have been solved before, and it often helps to see the way other people have written solutions to the same problems that you're facing in code. For example, if you're using Python, you might could try to find snippets using searches like these:

- 'How do I read input from the user in Python?'

- 'How do I iterate over characters in a string in reverse order in Python?'

- 'How to get the last character from a string in Python?'

- 'How do I output a number to the screen in Python?'

Code:

+----------+---+---+---+---+---+ | Position | 4 | 3 | 2 | 1 | 0 | +----------+---+---+---+---+---+ | Number | 4 | 6 | 9 | 2 | 3 | +----------+---+---+---+---+---+ +----------+--------+---------------------+ | Position | Number | Multiplier | Total | +----------+--------+------------+--------+ | 4 | 4 | 10,000 | 40,000 | | 3 | 6 | 1,000 | 6,000 | | 2 | 9 | 100 | 900 | | 1 | 2 | 10 | 20 | | 0 | 3 | 1 | 3 | +----------+--------+------------+--------+ | G R A N D T O T A L | 46,923 | +--------------------------------+--------+

Code:

Hex: 0 1 2 3 4 5 6 7 8 9 A B C D E F Dec: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Code:

Position 0 = 10^0 = 1 Position 1 = 10^1 = 10 Position 2 = 10^2 = 100 Position 3 = 10^2 = 1,000

Code:

Position 0 = 16^0 = 1 Position 1 = 16^1 = 16 Position 2 = 16^2 = 256 Position 3 = 16^3 = 65536

As far as writing code, consider the steps carefully for your Hex-to-Decimal algorithm. You cannot write code for an algorithm if you don't already understand the logical steps you need for that algorithm to work, so write those steps down using simplified plain-english (psuedocode) on paper to make sure you understand the sequence.

As far as writing code is concerned, If any problem or part of the problem seems too difficult, then break it down into a smaller problem, and if necessary, try entering the questions for your problems into Google or StackOverflow.

Most programming problems have been solved before, and it often helps to see the way other people have written solutions to the same problems that you're facing in code. For example, if you're using Python, you might could try to find snippets using searches like these:

- 'How do I read input from the user in Python?'

- 'How do I iterate over characters in a string in reverse order in Python?'

- 'How to get the last character from a string in Python?'

- 'How do I output a number to the screen in Python?'

1

reply

Report

#6

if it is python you can use int("number", 16)

16 refers to the number base, binary would be 2 and decimal is 10.

16 refers to the number base, binary would be 2 and decimal is 10.

0

reply

Report

#7

(Original post by

if it is python you can use int("number", 16)

16 refers to the number base, binary would be 2 and decimal is 10.

**Hammad(214508)**)if it is python you can use int("number", 16)

16 refers to the number base, binary would be 2 and decimal is 10.

0

reply

Report

#8

(Original post by

Consider the way you would do this for a decimal number. For example, the number 46923 breaks down like this:

Now all you need to do is replicate this for Hexadecimal instead. Remember that there are 16 possible values in a Hexadecimal digit ranging from 0-F (or 0-15):

Also remember how the multipliers of digits work in decimal, which is also-known-as Base-10 because there are 10 possible values from 0-9

Hexadecimal is also-known-as Base-16, because there are 16 possible values from 0-F, So Hexadecimal multipliers work like this:

So in summary, it's a simple matter of isolating each digit, based on its relative position from the right-hand-side, then applying the multiplier for its position.

As far as writing code, consider the steps carefully for your Hex-to-Decimal algorithm. You cannot write code for an algorithm if you don't already understand the logical steps you need for that algorithm to work, so write those steps down using simplified plain-english (psuedocode) on paper to make sure you understand the sequence.

As far as writing code is concerned, If any problem or part of the problem seems too difficult, then break it down into a smaller problem, and if necessary, try entering the questions for your problems into Google or StackOverflow.

Most programming problems have been solved before, and it often helps to see the way other people have written solutions to the same problems that you're facing in code. For example, if you're using Python, you might could try to find snippets using searches like these:

- 'How do I read input from the user in Python?'

- 'How do I iterate over characters in a string in reverse order in Python?'

- 'How to get the last character from a string in Python?'

- 'How do I output a number to the screen in Python?'

**winterscoming**)Consider the way you would do this for a decimal number. For example, the number 46923 breaks down like this:

Code:

+----------+---+---+---+---+---+ | Position | 4 | 3 | 2 | 1 | 0 | +----------+---+---+---+---+---+ | Number | 4 | 6 | 9 | 2 | 3 | +----------+---+---+---+---+---+ +----------+--------+---------------------+ | Position | Number | Multiplier | Total | +----------+--------+------------+--------+ | 4 | 4 | 10,000 | 40,000 | | 3 | 6 | 1,000 | 6,000 | | 2 | 9 | 100 | 900 | | 1 | 2 | 10 | 20 | | 0 | 3 | 1 | 3 | +----------+--------+------------+--------+ | G R A N D T O T A L | 46,923 | +--------------------------------+--------+

Code:

Hex: 0 1 2 3 4 5 6 7 8 9 A B C D E F Dec: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Code:

Position 0 = 10^0 = 1 Position 1 = 10^1 = 10 Position 2 = 10^2 = 100 Position 3 = 10^2 = 1,000

Code:

Position 0 = 16^0 = 1 Position 1 = 16^1 = 16 Position 2 = 16^2 = 256 Position 3 = 16^3 = 65536

As far as writing code, consider the steps carefully for your Hex-to-Decimal algorithm. You cannot write code for an algorithm if you don't already understand the logical steps you need for that algorithm to work, so write those steps down using simplified plain-english (psuedocode) on paper to make sure you understand the sequence.

As far as writing code is concerned, If any problem or part of the problem seems too difficult, then break it down into a smaller problem, and if necessary, try entering the questions for your problems into Google or StackOverflow.

Most programming problems have been solved before, and it often helps to see the way other people have written solutions to the same problems that you're facing in code. For example, if you're using Python, you might could try to find snippets using searches like these:

- 'How do I read input from the user in Python?'

- 'How do I iterate over characters in a string in reverse order in Python?'

- 'How to get the last character from a string in Python?'

- 'How do I output a number to the screen in Python?'

0

reply

(Original post by

What language are you using?

**jestersnow**)What language are you using?

0

reply

Report

#10

Or you can do it the really drawn out way which is totally not necessary but much more interesting imo if you are learning python (like me!)

Spoiler:

I would ask the user to input the hexadecimal digit in the form xy by asking them for x and y and assigning each digit to a variable,

Then I would set x to an integer and multiply x by 16 if x is a number. If x is a letter I would convert it to a number, then an integer and then multiply it by 16.

Then I would convert y to an integer if it is a number. If it is a letter I would convert it to a number and then an integer.

Then I would add x and y and show the result to the user.

If you get really stuck I can show you

Please remember this is a really drawn out method and you should not do this generally. I was guessing this is what your teacher was asking you to do purely because you have had a lot of time to do it. People before have posted the 'correct' way of doing it.

Show

I would ask the user to input the hexadecimal digit in the form xy by asking them for x and y and assigning each digit to a variable,

Then I would set x to an integer and multiply x by 16 if x is a number. If x is a letter I would convert it to a number, then an integer and then multiply it by 16.

Then I would convert y to an integer if it is a number. If it is a letter I would convert it to a number and then an integer.

Then I would add x and y and show the result to the user.

If you get really stuck I can show you

**part**of the code for it as I have just done it.

Please remember this is a really drawn out method and you should not do this generally. I was guessing this is what your teacher was asking you to do purely because you have had a lot of time to do it. People before have posted the 'correct' way of doing it.

0

reply

X

Page 1 of 1

Go to first unread

Skip to page:

### Quick Reply

Back

to top

to top