2Warm – picoCTF 2019

Challenge

Can you convert the number 42 (base 10) to binary (base 2)?

Hint

Submit your answer in our competition’s flag format. For example, if you answer was ‘11111’, you would submit ‘picoCTF{11111}’ as the flag.

Solution

As always, there are multiple ways to arrive at the solution. In this walkthrough I’ll go through the easiest and most useful methods.

Method 1: Online Converter

Once again, the trusted online converter can easily handle conversions like this. We’ll use RapidTables to solve this.

Another online method we’ve used before is the ASCII table. In this case, ASCII tables do not typically display binary. Instead, we can manually convert by doing the math.

41 = 1(2^5) + 0(2^4) + 1(2^3) + 0(2^2) + 1(2^1) + 0(2^0) = 101010

Method 2: Python

The Python function “bin” is built into Python3 and is the quickest way to convert decimal to binary without creating our own function.

Python2 also has the bin function built into a default library:

Method 3: Bash

Unfortunately, there is no extremely simple way to convert decimal to binary in bash. Since C programming does not have a %b (binary) identifier, we have two options: write a script and save it for later on the shell (assuming you have file creation permissions), or write the following script that I will explain in more depth:

First, we are creating an array that contains each permutation of an 8-bit binary number (00000000, 00000001, etc.) which is then stored into the variable Dec2Bin. When we search for the index of “42” within the array, we get 00101010 returned to us because the permutations are in order, and thus the 42nd index is equivalent to our binary value. If we use the command ${Dec2Bin[@]}, we can see all values within the array, showing us each permutation in order:

There are probably better ways of displaying all of the items, but we can see most of them here.

Remember that since this binary array is 8 bits, we can get rid of the leading 0’s as they are irrelevant to finding the value here.

Flag

The flag is picoCTF{101010}



Previous: Warmed UpNext: Bases