Hexidecimal to decimal

Mathematics and Science; the subtle and ubiquitous arts

Hexidecimal to decimal

Post by tripbeam on Mon Nov 03, 2014 6:27 pm
([msg=84769]see Hexidecimal to decimal[/msg])

Can somone please give me an example on how to convert a hexidecimal number to a decimal number? i have been looking around but still don't get it.

0x62ee6190

is the problem i am trying to solve it's from the pico challenge, i could get the answer and pass it easily using an online converter but i want to be able to convert it myself. the steps i'm doing are as follows and i'm leaving out the 0x at the beggining as those just tell me that it's hexidecimal ( ^ <--- that arrow means root right? )

6 2 14 14 6 1 9 0

1) 6 * 16 ^ 7 = 254

2) 2 * 16 ^ 6 = 78

3) 14 * 16 ^ 5 = 500

4) 14 * 16 ^ 4 = 448

5) 6 * 16 ^ 3 = 166

6) 1 * 16 ^ 2 = 22

7) 9 * 16 ^ 1 = 144

8) 0 * 16 ^ 0 = 0

The answer i get after adding all those up is 1612 but in the converter online the answer is 1659789712 and i don't understand why it's such a high number, i know i must be missing a step.

Thank you to anyone who explains this in advance
tripbeam
Poster
Poster
 
Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by akorshikai on Mon Nov 03, 2014 7:03 pm
([msg=84770]see Re: Hexidecimal to decimal[/msg])

Check your math, start with line #6. What's 16 squared? Certainly not 22. Go from there for line 5 on backwards. I haven't verified the rest, just a glaring issue with line 6.
akorshikai
Experienced User
Experienced User
 
Posts: 71
Joined: Sat Jun 28, 2014 8:21 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by cyberdrain on Mon Nov 03, 2014 7:24 pm
([msg=84771]see Re: Hexidecimal to decimal[/msg])

Yeah, akorshikai is right, I found that calculating 16^7*6 you get a number close to the online converter, confirming that your math is the problem.

Edit: I can now confirm that the math is indeed the problem, I got the same answer as the online converter by doing the calculation correctly.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by tripbeam on Mon Nov 03, 2014 10:01 pm
([msg=84772]see Re: Hexidecimal to decimal[/msg])

Alright thanks, i will try that when i get back too it.
tripbeam
Poster
Poster
 
Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by -Ninjex- on Tue Nov 04, 2014 5:09 am
([msg=84774]see Re: Hexidecimal to decimal[/msg])

Let's take an example Hexadecimal number: 8A4C

The first thing you need to realize is this is base 16, and we want it in base 10.
To do this, count the amount of digits in the number and subtract one, in this case "8A4C" would be 3.
Next, convert all the characters in the number to their base 10 equivalent.
This give us: 8, 10, 4, 12

This is where the number 3 comes in handy, as each number needs to be given the power of it's current position.
For example, 8^3, 10^2, 4^1, 12^0

Now, the next step would be to make sure you are multiplying by 16:
8*16^3, 10*16^2, 4*16^1, 12*16^0

8*16**3 = 32768
10*16**2 = 2560
4*16**1 = 64
12*16**0 = 12

Last thing to do is add the values
32768+2560+64+12 = 35404

For a live demonstration, check this out:
http://repl.it/3K8
Here is the code from above, if you are scared of links (Ruby)
Code: Select all
hex = {'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15}
value = '62ee6190'

numbers = value.split('')
@total, @c = 0, ((value.length)-1)
numbers.each do |n|
  if n =~ /[[:alpha:]]/
    n = hex[n]
  else
    n = n.to_i
  end
  puts "#{n} * 16 ** #{@c} = #{n * 16 ** @c}"
  @total += (n * 16 ** @c)
  @c -= 1
end
puts @total
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by Randoph on Tue Nov 04, 2014 8:10 am
([msg=84775]see Re: Hexidecimal to decimal[/msg])

-Ninjex- wrote:Let's take an example Hexadecimal number: 8A4C
...
Last thing to do is add the values
32768+2560+64+12 = 35404

Thanks a lot Ninjex. I was struggling on this as well but this pretty much explains it.
One question tho, why do you count the numbers from right to left? Wouldn't it be more logical that the 8 is in the 1st(0th) posistion? That way you get:

8*16^0 = 8
10*16^1 = 160
4*16^2 = 1024
12*16^3 = 49152

8+160+1024+49152 = 50344
A little rebellion now and then ... is a medicine necessary for the sound health of government.
-Thomas Jefferson
User avatar
Randoph
Poster
Poster
 
Posts: 127
Joined: Fri Aug 08, 2014 2:48 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by -Ninjex- on Tue Nov 04, 2014 8:49 am
([msg=84776]see Re: Hexidecimal to decimal[/msg])

Randoph wrote:One question tho, why do you count the numbers from right to left? Wouldn't it be more logical that the 8 is in the 1st(0th) posistion? That way you get:

8*16^0 = 8
10*16^1 = 160
4*16^2 = 1024
12*16^3 = 49152

8+160+1024+49152 = 50344


Well, 8 is the first digit, but it has the highest power.
Just like the decimal value '110' break it down
1 * 10 ** 2 = 100
1 * 10 ** 1 = 10
0 * 10 ** 0 = 0
Answer 110

100 has a power of 2 (two places away from the first digit)
10 has a power of 1 (one place away from the first digit)
0 has a power of 0 (it's the first digit)

Looking at this wiki, may help you:
http://en.wikipedia.org/wiki/Power_of_10

So for hexadecimal, we just substitute the 10 with 16

If you do it in reverse, you will be telling it that 100 has a power of 0, 10 has a power of 1, and 0 has a power of 2, which is false, and will lead to an incorrect result as you can see from your above example.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by Randoph on Tue Nov 04, 2014 9:06 am
([msg=84777]see Re: Hexidecimal to decimal[/msg])

-Ninjex- wrote:
Randoph wrote:One question tho, why do you count the numbers from right to left? Wouldn't it be more logical that the 8 is in the 1st(0th) posistion? That way you get:

8*16^0 = 8
10*16^1 = 160
4*16^2 = 1024
12*16^3 = 49152

8+160+1024+49152 = 50344


Well, 8 is the first digit, but it has the highest power.
Just like the decimal value '110' break it down
1 * 10 ** 2 = 100
1 * 10 ** 1 = 10
0 * 10 ** 0 = 0
Answer 110

100 has a power of 2 (two places away from the first digit)
10 has a power of 1 (one place away from the first digit)
0 has a power of 0 (it's the first digit)

Looking at this wiki, may help you:
http://en.wikipedia.org/wiki/Power_of_10

So for hexadecimal, we just substitute the 10 with 16

If you do it in reverse, you will be telling it that 100 has a power of 0, 10 has a power of 1, and 0 has a power of 2, which is false, and will lead to an incorrect result as you can see from your above example.

Thank makes a lot of sence.
A little rebellion now and then ... is a medicine necessary for the sound health of government.
-Thomas Jefferson
User avatar
Randoph
Poster
Poster
 
Posts: 127
Joined: Fri Aug 08, 2014 2:48 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by tripbeam on Tue Nov 04, 2014 2:03 pm
([msg=84785]see Re: Hexidecimal to decimal[/msg])

Thank you guys! i finnaly understand it, took me ages
tripbeam
Poster
Poster
 
Posts: 166
Joined: Tue Jul 01, 2014 8:52 pm
Blog: View Blog (0)


Re: Hexidecimal to decimal

Post by -Ninjex- on Tue Nov 04, 2014 6:19 pm
([msg=84789]see Re: Hexidecimal to decimal[/msg])

tripbeam wrote:Thank you guys! i finnaly understand it, took me ages

No problem, trip. Glad you asked, I never mind helping :D
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Next

Return to Math & Science

Who is online

Users browsing this forum: No registered users and 0 guests