The Student Room Group

Help with Computer Science homework?

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."
(edited 6 years ago)
Original post by 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."


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.
Original post by 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.

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


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


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


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


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


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

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


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

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


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?'
(edited 6 years ago)
if it is python you can use int("number", 16)
16 refers to the number base, binary would be 2 and decimal is 10.
Original post by 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.


Yeah that's why I asked, in Python (and a few other languages) it's a piece of cake.
Original post by winterscoming
Consider the way you would do this for a decimal number. For example, the number 46923 breaks down like this:



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



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



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



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


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



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


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



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


Great answer! You've done it for the OP more or less.
Original post by jestersnow
What language are you using?


We're being told to use python. Our due date for the homework has been extended but I'm still stuck.
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

Quick Reply

Latest

Trending

Trending