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 trial

JavaScript JavaScript Foundations Numbers Creating Numbers: Part 2

Harry James
Harry James
14,780 Points

Hexidecimal 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
James Barnett
39,199 Points

note: When converting base16, 10 = 16 so then F = 15

Working right to left

  1. Add up the 1s place
    • F in the 1s place is 15 so 15 x 1 = 15
  2. Add up the 16s place
    • F in the 16s place is 15 so 15 x 16 = 240
  3. 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.

Harry James
Harry James
14,780 Points

I 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!)

Hexidecimal 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
Harry James
14,780 Points

Thanks! Appreciate the explanation, makes sense now!