picoCTF 2019 2Warm Writeup

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^72^62^52^42^32^22^12^0
1286432168421
Base 10 to binary

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.

0000081000
1000191001
20010a1010
30011b1011
40100c1100
50101d1101
60110e1110
70111f1111
Hexadecimal to binary

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s