Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHarry James
14,780 PointsHexidecimal Numbers
Hello,
I was looking into Hexidecimal numbers however, I'm not quite as to sure how they work.
I understand 0x then 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15,16 and following this sequence, F is equal to 15 (0xF). However, how is 0xFF equal to 255? As, 15+15 is 30 and 15*15 is 225.
I'm not using this in any program however, am interested in how this actually works.
Thanks in advance!
2 Answers
James Barnett
39,199 Pointsnote: When converting base16, 10 = 16
so then F = 15
Working right to left
- Add up the
1s
place-
F
in the1s place
is15
so15 x 1 = 15
-
- Add up the
16s
place-
F
in the16s place
is15
so15 x 16 = 240
-
- Get the sum of the previous steps
15 + 240 = 255
I found the above explanation kinda confusing as it's been a while since I worked with base conversion.
So this is my process if that helps any future forum searchers.
Cena Mayo
55,236 PointsHexidecimal is a base-16 system. You remember in decimal how you refer to the hundreds place, tens place, ones place? Well, in hex, it's the same thing, except it looks like this: 16s place, 1s place.
(OxF == F. Adding digits to the left doesn't change the number.)
So what's actually happening with FF, for example, is that it's saying that there are 15 16-places (15*16=240) and 15 1s (=15). Adding these together gets you to 255.
Hope that clarifies a bit!
Harry James
14,780 PointsThanks! Appreciate the explanation, makes sense now!
Harry James
14,780 PointsHarry James
14,780 PointsI have updated my Best Answer now (Yet still credited the previous response) and was able to test this out with some other characters (Like AA). Works perfectly! I went through the steps you said however, instead using the characters AA and, I got the result of 170. Checking this in my Chrome window, I got the same result.
Appreciate the response and I'll note these steps down incase I ever need a base16 conversion (Probably for reading code, I doubt I'll write in base16, I think I'll get myself confused. Haha!)