1

FIRSTLY, I hope this is the right forum to ask this question, otherwise please, tell me where should I re-post it or move it(if possible).

So I have a situation in which I need to convert the "decimal" 74 to hexadecimal 4A.

I currently know how to convert that 4A to 74 which is pretty easy, what I'm having troubles with is how to get the 4A from 74?

Can somebody please tell me how to do it?So far the only thing I know is how to get the first number(inn this case is 4) by doing this:

74/16= 4.625 (we only care about the numbers before the dot; 4)

4*16 = 64

now to get the remainder, we subtract 74-64

74-64= 10(this is the remainder).

my PROBLEM comes here, I have seen a lot of videos doing the same thing but I don't know why I keep getting these results:

from the last steps we bring the number 10(remainder) to divided by 16 and get the second hex term.

10/16= 0.625(we only care about the first number before the dot, right?)

0.625 x 16 = 10

then we bring the first remainder again(10) to subtract it with the second remainder which is 10 as well, so:

10-10 = 0.(this should be the third hex number)

So pretty much the result we get is this:

4,0,0... how in the world the two zeros can represent the A? because is 10.

Any help would be very appreciated . I hope I could make myself clear.

Eric Wofsey
  • 330,363
Kirasiris
  • 113

2 Answers2

0

You're really almost there. The $10$ is less than $16$ so it's already just a single hex digit, $A$. Then $$ 74_{10} = 4A_{16}. $$

You should check that by working backwards.

Of course if you started with a number greater that $256$ you would have to subtract a multiple of $256$ first, to get the digit in the third place.

There is in fact a better right-to-left algorithm to find the digits in the other order. Try it for $300$. You start as you did by dividing by $16$, with an integer remainder, not a decimal fraction: $$ 300 = 18 \times 16 + 12. $$ That tells you the units hex digit is $C$.

Now subtract that remainder and divide by $16$ to get $18$. That's clearly $12_{16}$ so your answer is $300 = 12C_{16}$. (If it weren't clear you'd just do the same thing again - divide by $16$ and use the remainder as the next digit you're looking for.)

Edit in response to comment.

Here's another explanation. Suppose you have an integer variable $n$ in a computer program. You want to print out its value, but the only print statement in the language prints just a single digit in the range 0..9. But you can do all the arithmetic you want.

Compute $n$ modulo $10$ - the remainder $r$ when you divide $n$ by $10$. (That might be the command d = n % 10 .) Then $d$ is the units digit of the $n$, so you can print d .

Now calculate $(n - d)/10$, which will be an integer (no remainder). Make that your new value of $n$. The units digit for that new value is the $10$'s digit of the original, so you can do the whole calculation over again to get that next digit.

Stop when the new $n$ is $0$. You will have printed out the number from right to left, units digit first.

Now as far as the computer is concerned there's nothing special here about $10$. You can use the same algorithm for any base, as long as you can print the digits in that base. It's what I just did for base $16$.

In fact it's likely that internally the computer is storing the value of $n$ in binary. Moreover, the language probably has a built in function that does just this, and then arranges the digits in the correct order. In the C language that function is called itoa, for "integer to ascii"/

Ethan Bolker
  • 95,224
  • 7
  • 108
  • 199
  • Sorry man, I just started to see this yesterday and I'm having some troubles with it. So when you say 10 is less than 16, you're referring to the base 10 in 74?; so pretty much the 74 = 4? and then the base 10 equals to A?... just like that?. sorry for any inconvenience or some kind of dumb comment I may write, I just want to make sure I grasp all this. – Kirasiris Mar 07 '18 at 01:33
  • You have the right idea, but your language is not right. "base $10$ in $74$" makes no sense. I've added a few paragraphs to my answer; hope they help. – Ethan Bolker Mar 07 '18 at 14:12
0

We start with seventy-four items grouped for base ten (seven tens and four ones):

x x x x x x x x x x

x x x x x x x x x x

x x x x x x x x x x

x x x x x x x x x x

x x x x x x x x x x

x x x x x x x x x x

x x x x x x x x x x

x x x x

We then regroup for hexadecimal (sixteens and ones):

x x x x x x x x x x x x x x x x

x x x x x x x x x x x x x x x x

x x x x x x x x x x x x x x x x

x x x x x x x x x x x x x x x x

x x x x x x x x x x

That's four groups of sixteen and ten ones. In hexadecimal, A is the symbol for ten items in a place. So this gives you $4A$ as the hex representation of your number.

As noted in Ethan Bolker's answer, you can get the digits right-to-left by looking at remainders upon successive division by sixteen.

paw88789
  • 40,402
  • If you have 20 sixteen groups and 11 ones for example. What do you do you do then? – Ariel Fishbein Mar 30 '24 at 16:17
  • @ArielFishbein You group up $16$ of the $20$ sixteen groups, giving one group of $16^2$ and four groups of $16$. This would give $14A$ as your hex numeral (assuming that there were still ten ones). – paw88789 Mar 30 '24 at 18:27
  • Ohh I get it. So What I need to do is $x_0\cdot 16^0+x_1\cdot 16^1+\cdots+x_n\cdot 16^n_{base 10}=x_0x_1x_2\cdots x_n_{base 16}?$ Does it work If I want to do base 2 to base 16? - For some reason the syntax doesnt work – Ariel Fishbein Mar 30 '24 at 18:53
  • Base two to base sixteen (and vice versa) is especially easy because base two and base sixteen are compatible in the sense that four bits exactly represents one hex digit. – paw88789 Mar 30 '24 at 21:00
  • Ohh I see why. Thank you! – Ariel Fishbein Mar 30 '24 at 22:29