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
Gordon Jiroux
8,728 PointsConverting To And From Decimal, Binary Hexidecimal, Octal
For those of you who are unfamiliar with converting decimal to hexadecimal or octal, I'm going to show you ho it was taught to me when I was 7. The key is to convert to binary first. (Treehouse Staff, hopefully you could do a video on this as it's easier to show in a non-static visual format)
- A quick look at Decimal to set the stage. Decimal is a numbering system that uses digits 0 through 9, and when you increment passed 9, you carry the 1, and reset the right digit to 0... Think of it like an odometer, 00,01,02,....09,10,11....18,19,20,21 etc.
This is no different when incrementing in any other numbering system, the only difference id what digit range you have to work with:
Dec = 0-9 = 10
Bin = 0-1 = 2
Oct = 0-7 = 8
Hex = 0-9 & A-F = 16
The first step is converting to binary (1s and 0s) is to understand that binary is a numbering system just like decimal wherein position of the digits relates to a value multiple. In decimal each position to the left is a multiple of 10 (1000, 100, 10, 1). In binary every position to the left is a multiple of 2 (8, 4, 2, 1) the only other difference is that instead of having 0-9, we only lave 0 and 1. the position indicates what that value represents.
Let's say we have a decimal number 7:
8 4 2 1
_________________________
0 1 1 1
4 + 2 + 1 = 7
Now let's say we have the number 147: start from the left and subtract, and ask yourself, will subtracting the position number from the remainder result in a non negative number? ( greater than or equal to 0)
128 64 32 16 8 4 2 1
_______________________________________________________________
1 0 0 1 0 0 1 1
Can we take 128 from 147? Yes, so the 128 position gets a 1... what's left over? 147 - 128 = 19
147 - 128 = 19 => 1
19 - 64 = -45 => 0
19 - 32 = -13 => 0
19 - 16 = 3 => 1
3 - 8 = -5 => 0
3 - 4 = -1 => 0
3 - 2 = 1 => 1
1 - 1 = 0 => 1
147 decimal = 10010011 binary
CONVERT BINARY TO HEX (Group on every 4 bits, otherwise known as a "nibble" (starting from the right))
1001 0011
For hex, we start the position over every 4 bits like this...
8 4 2 1 8 4 2 1
______________ ______________
1 0 0 1 0 0 1 1
147 in Decimal = 10010011 in Binary = (8 + 1) and (2 + 1) = 9 and 3, or 93 Hex
CONVERT BINARY TO OCTAL (Group on every 3 bits (starting from the right))
(0)10 010 011 (leading empty spaces = 0)
For octal, we start the position over every 3 bits like this...
4 2 1 4 2 1 4 2 1
___________ __________ ___________
0 1 0 0 1 0 0 1 1
147 in Decimal = 10010011 in Binary = 2 and 2 and (2 + 1) or 223 Octal
You can convert to and from any of these numbering systems very easily, as long as you go to binary first.
1 Answer
Gordon Jiroux
8,728 PointsThe formatting on the grouping is jacked, I'm efforting to fix it, but it's not playing nice ATM.