2Warm is a General Skills puzzle worth 50 points.
Description
Can you convert the number 42 (base 10) to binary (base 2)?
Solution
This puzzle is trivial to solve using an online tool such as RapidTables, but I figured I’d show how to do this by hand.
The process of doing this manually is relatively straightforward, but explaining it is a challenge for me, so I will attempt to do so now.
Base 10 to Binary
The following chart represents the powers of 2 for 8 bits (one byte). The maximum value of one byte in decimal is 255: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255
Since 42 is less than 1 byte, this chart is enough. If you are working with bigger numbers, extend this chart accordingly.
2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Now, work from left to right on this chart. If the number you wish to convert to binary is greater than or equal to the value in the column, place a 1 in this position. Otherwise, place a 0. Subtract the value in the column from the original number, and keep working your way to the right, carrying the remainder:
- 42 is less than 128 = 0
- 42 is less than 64 = 0
- 42 is greater than 32 = 1
- 42 – 32 = 10
- 10 is less than 16 = 0
- 10 is greater than 8 = 1
- 10 – 8 = 2
- 2 is less than 4 = 0
- 2 is equal to 2 = 1
- 2 – 2 = 0
- 0 is less than 1 = 0
This leaves us with 00101010
. Removing the leading zeroes, the answer is 101010
. Encompassing this with picoCTF{}
yielded the correct flag.
To check that this is correct, 32 + 8 + 2 = 42.
Binary to Hexadecimal
We don’t need to do this to solve the puzzle, however when working on similar challenges and programming, converting binary to hex and vice versa comes in handy often.
In the previous section, we determined that 42 is 00101010 in binary. To convert a binary number to hex, I use a chart like the one below. Since I run into the need to convert hex to binary and vice versa so often, I have committed this table to memory.
You must make sure that the number of binary digits you are working with is cleanly divisible by 8. If it isn’t, add leading zeroes until it is.
For example, I added the two leading zeroes from the previous example because 42 represented as binary is 6 digits (101010). Adding the leading zeroes brings it to 8 digits (00101010).
Next, you simply refer every 4 bits (a nibble), to their corresponding hexadecimal value. 00101010
broken down into nibbles is 0010
and 1010
. Referring to the chart, 0010 is 2
, and 1010 is a
. Therefore, 42 = 0x2a.
0 | 0000 | 8 | 1000 |
1 | 0001 | 9 | 1001 |
2 | 0010 | a | 1010 |
3 | 0011 | b | 1011 |
4 | 0100 | c | 1100 |
5 | 0101 | d | 1101 |
6 | 0110 | e | 1110 |
7 | 0111 | f | 1111 |
Converting hex to binary is possible with the same chart. Each hexadecimal digit represents a nibble. Simply refer to the chart to get the binary representation of each nibble. 2 = 0010, a = 1010. 2a = 00101010.
Pingback: picoCTF Writeups – DMFR SECURITY