Description
Can you convert the number 42 (base 10) to binary (base 2)?
Solution
Walk me through it- Step 1Convert decimal to binary with PythonOpen a Python shell and call bin(42). Python returns '0b101010' - the '0b' prefix signals binary notation and is not part of the answer. Strip it off: the flag is 101010.python
python3 -c "print(bin(42))"Learn more
Binary (base 2) is the numeral system used internally by every digital computer. Each digit, called a bit, can only be 0 or 1, corresponding to off and on states in electronic circuits. Groups of 8 bits form a byte, the fundamental unit of data storage and transmission.
Converting from decimal to binary is done by repeatedly dividing by 2 and recording remainders. For 42: 42 ÷ 2 = 21 R0, 21 ÷ 2 = 10 R1, 10 ÷ 2 = 5 R0, 5 ÷ 2 = 2 R1, 2 ÷ 2 = 1 R0, 1 ÷ 2 = 0 R1. Reading the remainders from bottom to top gives
101010. Python'sbin()automates this and prepends0bto signal the base.Knowing how to convert between number bases is essential in CTF and security work. Hexadecimal (base 16) is used for memory addresses and color codes. Octal (base 8) appears in Unix file permissions. Binary shows up directly in bitwise operations, network subnetting, and binary exploitation.
bin(n)- decimal to binary string in Pythonhex(n)- decimal to hex string in Pythonoct(n)- decimal to octal string in Pythonint('101010', 2)- binary string back to decimal
Bitwise operations - AND, OR, XOR, NOT, left shift, and right shift - all operate directly on the binary representation of numbers. XOR (
^in Python) is particularly important in cryptography and CTF challenges: XOR-ing a value with itself gives 0, XOR-ing with 0 gives the original value, and XOR is its own inverse (applying it twice cancels out). Many simple encryption schemes use XOR with a repeating key, which is vulnerable to frequency analysis when the key is short.Binary in network subnetting is another essential application. An IPv4 address like 192.168.1.1 is really four binary octets. A subnet mask like 255.255.255.0 (or /24 in CIDR notation) is a sequence of 24 ones followed by 8 zeros in binary:
11111111.11111111.11111111.00000000. ANDing an IP address with its subnet mask gives the network address. Understanding subnets at the binary level makes network security concepts like ACLs, firewall rules, and CIDR ranges much more intuitive.Signed vs. unsigned integers is a critical distinction in binary. An 8-bit unsigned integer represents 0-255. An 8-bit signed integer (two's complement) represents -128-127 - the most significant bit signals the sign. The value
11111111in binary is 255 as unsigned but -1 as a signed 8-bit integer. This ambiguity is the source of many real-world vulnerabilities: integer overflows, sign confusion bugs, and type confusion errors in C/C++ code often stem from the boundary between unsigned value 128 (binary10000000) being reinterpreted as -128 in a signed context.
Alternate Solution
No Python handy? Use the Number Base Converter built into this site. Enter 42 in the decimal field and it instantly shows the equivalent in binary, hex, and octal - all at once.
Flag
picoCTF{...}
Binary is base-2 - each digit is a power of 2. Python's bin() function shows a '0b' prefix which is not part of the answer.