Binary decoding
Content:
[list]
[]1. Introduction.
[]2. Binary <
[]3. Binary
[]4. Binary <
[]5. Loading…
[]X. Side information.
[/list]
1. Introduction:
In this tutorial, I’m going to show you how to decode Binary, also called Base-2. This is used in a few challenges on hacking sites, like Main level 8 on Hackthis.
Binary is a numeral system made up with the numbers 0 and 1, and is used by almost all modern computers to think and communicate.
In the computer 1 represents signal ON, and 0 represents signal OFF.
Each binary number represents 1 bit, and when put together in groups of 8, they represent 1 byte. These bytes are then being stored or used for different purposes.
2. Binary
To convert from Binary to Decimal or the other way around, we start with a number.
Example number: 65 in Decimal.
Divide:
Divide the number with 2, until you divide ½. If you get a number with a comma, you type 1 and if you get a number without comma you type 0. Here you don’t round up when you get something like 32,5. Normally you would round up to 33, but here we round down to 32, so you always divide with a complete number.
Example:
```65/2 = 32,5 = 1
32/2 = 16 = 0
16/2 = 8 = 0
8/2 = 4 = 0
4/2 = 2 = 0
2/2 = 1 = 0
½ = 0,5 = 1
Read from ½ and up: 1000001 is 65 written in Binary.```
On/Off:
On/Off method works both ways.
Binary is used to control whether a bit is turned on or off. 1 is on and 0 is off.
You decode a binary number from right to left and then add the bits starting from right with 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 and so on… You then take all the bits which are turned ON and add them together, to get the decimal number.
Example:
```
1 0 0 0 0 0 1
64 32 16 8 4 2 1
1 and 64 is turned ON, so add them together: 64+1 = 65
To do it from decimal to binary, you just extract the biggest possible bit number, until you have nothing left.
When it's possible to extract, without getting a negative result, you do it, and then write 1, if you can't extract the number, without getting a negative result, you write 0 and skips it.
Example:
65-64 = 1 = 1
1-32 = -31 = 0
1-16 = -15 = 0
1-8 = -7 = 0
1-4 = -3 = 0
1-2 = -1 = 0
1-1 = 0 = 1
This gives you 1000001, which is 65 written in binary.
```
3. Binary <
To convert Binary to Text and the other way around, we have to go through two steps. First convert it to its decimal number in ASCII (American Standard Code for Information Interchange) chart.
Second convert that decimal number to Binary. (So if it’s Binary, convert it to a decimal number, and then figure out what letter that number belongs to).
Here you can see all the letters with their decimal numbers (and some a lot more).
ASCII chart.
If you doesn’t have a chart to look at, you can always start from uppercase A = 65 or lowercase a = 97, and then just count your way up through the alphabet (a =97, b = 98, c = 99, d = 100. A = 65, B = 66, C = 67 and so on….)
(remember to watch for upper and lowercase letters).
Example:
```
Kabue written in Binary.
K = 75 = 1001011
a = 97 = 1100001
b = 98 = 1100010
u = 117 = 1110101
e = 101 = 1100101
Kabue = 1001011 1100001 1100010 1110101 1100101
(You could type all the binary numbers with all their 8 digits, example K = 01001011, but to make is simple I’ve removed the front zero. So if someone writes 2 decimal = 10 binary, it’s the same as 2 decimal = 00000010 binary, but to make it pretty and easy, we remove the front zeroes (which is just bits turned OFF)).
```
4. Binary > Hexadecimal
Hexadecimal, also known as Base-16, use 16 symbols before adding a new.
It uses 0-9 like decimal (base-10), and then starts with A-F, representing 10-15. When converting from Binary to Hex, each Hex digits, is represented by 4 binary digits. So we simply take them one by one.
Scheme over Hex values:
Hexadecimal - Decimal - Binary
1 - 1 - 1
2 - 2 - 10
3 - 2 - 11
4 - 4 - 100
5 - 5 - 101
6 - 6 - 110
7 - 7 - 111
8 - 8 - 1000
9 - 9 - 1001
A - 10 - 1010
B - 11 - 1011
C - 12 - 1100
D - 13 - 1101
E - 14 - 1110
F - 15 - 1111
Okay, we can now start decoding big numbers from Hex to Binary, and from Binary to Hex. When decoding the digits one by one, you simply use the method from Decimal <
Example:
Hex : 2F5C91
2 = 0010
F = 1111
5 = 0101
C = 1100
9 = 1001
1 = 0001
Binary : 0010 1111 0101 1100 1001 0001
(Decimal : 3103889)
X. Side information. Nothing with Binary to see here…
Hexadecimal
Hex
This isn’t as easy to do in your head as Binary
When we have a Hex number, like 21C, we start with C. C represents 12 in Decimal, and there’s only one of them. To show this in the system I’m going to use, we take the first digit ©, which is 12, and multiply it with 160 (12160 = 12).
Second digit (1) represent 1 in Decimal, take this and multiply it with 161 (1161 = 16).
Third digit (2) represent 2 in Decimal, take this and multiply it with 162 (2*162 = 512).
Then you add all the results together, and you get the finishing Decimal value: 12 + 16 + 512 = 540.
(Hope you see the system, if not: For each digit you have, you add 1 to the raising number (not sure if this is the right way to say it).
Example:
Hex : FA85
5 = 5*16^0 = 5
8 = 8*16^1 = 128
A = 10*16^2 = 2560
F = 15*16^3 = 61440
Decimal : 64133
Decimal -> Hex
This is almost the same system as before, except this time it’s backwards, so instead of multiplying, you divide.
So we start out with Decimal : 786. You then divide it with the biggest possible number in the 16x system, without the result getting less then 1.
If you get an incomplete result, you round down, and substract the used 16xrounded down result, to get the leftovers.
So it’s 786/162 = 3 -> leftovers = 786-3162 = 18.
Then you take leftovers and repeat the same process as before. You do this until leftovers is 0 (zero).
18/161 = 1 -> leftovers = 18-1161 = 2.
2/160 = 2 -> leftovers = 2-2160 = 0.
Then you write the results in order: 312, which is 786 Decimal, written in Hex.
More coming later, when I learn more about binary or someone wants to add something to the tutorial, or correct me if I’ve mistaken something, feel free to do so.
Hope you liked it, and have fun.
-Kabue
Need help with math homework? Hit me up! I can help out with integral calculus and below.
For the binary conversion, wouldn’t it be better to have an array filled with powers of 2 (1,2,4,8,16,32,64, and maybe 128 is it’s needed) and then basically use another array marked as used and tick off the number used and then subtract it? Like this:
int two[8] = {128,64,32,16,8,4,2,1};
int i,j;
int used[8] = {0,0,0,0,0,0,0,0};
string word;
cin >> word;
for(j=0; j<word.length(); j++) {
for(i=0; i<8; i++) {
if(two[i] < word[j]) {
used[i] = 1;
word[j] = word[j] - two[i];
}
}
for(i=0; i<n; i++) {
cout << used[i];
}
}
cout << endl;
That is c++ btw, and I just wrote the code inside int main();
But great job Kabue on making this.
You should really make this an article instead of a forum post, I think a lot more will benefit from this that way.
Wibben, great example of this in C++! I think he was just trying to show a basic example of binary conversion :). Again though, great example of usage within C++!
Need help with math homework? Hit me up! I can help out with integral calculus and below.
Thanks Ski900, I just thought maybe someone who is looking at this page would want to have a piece of example code to explain how the conversion is implemented.
Ahhh, my bad then, I misunderstood what you said! It made me think though, some tutorials of the absolute basics would be nice on here. Such as understanding the body of a program, loops, so on and so forth.
Need help with math homework? Hit me up! I can help out with integral calculus and below.
Speaking of teaching the basics… should I just make an article on c++ just to help teach people and maybe earn a medal? Sorry for going off topic from the binary conversion thing.
Need help with math homework? Hit me up! I can help out with integral calculus and below.
Content is spelled wring and you forgot to add Binary <-> Hexadecimal in the Contents… Probably just me trying to pick a fishbone from an egg
A HACKER should see through his DeCeptions ………………………………..
I guess I see through my DeCeptions ………………………………………………..
11 years ago
0
I love binary. :D