# Help with Computer Science homework?

Watch
Announcements
#1
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."
1
2 years ago
#2
(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.
0
#3
(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. :/
0
2 years ago
#4
(Original post by xxJiminiePaboxx)
Thanks for explaining it. I'm just still stumped on writing the program. :/
What language are you using?
0
2 years ago
#5
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 |
+--------------------------------+--------+```
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):
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```
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
Code:
```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:
Code:
```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?'
1
2 years ago
#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.
0
2 years ago
#7
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.
0
2 years ago
#8
(Original post by 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 |
+--------------------------------+--------+```
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):
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```
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
Code:
```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:
Code:
```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.
0
#9
(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.
0
2 years ago
#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:
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
X

new posts Back
to top
Latest
My Feed

### Oops, nobody has postedin the last few hours.

Why not re-start the conversation?

see more

### See more of what you like onThe Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

### Poll

Join the discussion

#### Are you travelling in the Uni student travel window (3-9 Dec) to go home for Christmas?

Yes (91)
28%
No - I have already returned home (43)
13.23%
No - I plan on travelling outside these dates (62)
19.08%
No - I'm staying at my term time address over Christmas (32)
9.85%
No - I live at home during term anyway (97)
29.85%