> For the complete documentation index, see [llms.txt](https://cajac.gitbook.io/ctf-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cajac.gitbook.io/ctf-notes/conversions/binary-numbers.md).

# Binary numbers

A binary number is a [number](https://en.wikipedia.org/wiki/Number) expressed in the [base](https://en.wikipedia.org/wiki/Radix)-2 [numeral system](https://en.wikipedia.org/wiki/Numeral_system) or binary numeral system, a method for representing [numbers](https://en.wikipedia.org/wiki/Number) that uses only two symbols for the [natural numbers](https://en.wikipedia.org/wiki/Natural_number): typically "0" ([zero](https://en.wikipedia.org/wiki/Zero)) and "1" ([one](https://en.wikipedia.org/wiki/One)). A *binary number* may also refer to a [rational number](https://en.wikipedia.org/wiki/Rational_number) that has a finite representation in the binary numeral system, that is, the quotient of an [integer](https://en.wikipedia.org/wiki/Integer) by a power of two.

The base-2 numeral system is a [positional notation](https://en.wikipedia.org/wiki/Positional_notation) with a [radix](https://en.wikipedia.org/wiki/Radix) of [2](https://en.wikipedia.org/wiki/2). Each digit is referred to as a [bit](https://en.wikipedia.org/wiki/Bit), or binary digit. Because of its straightforward implementation in [digital electronic circuitry](https://en.wikipedia.org/wiki/Digital_electronic_circuit) using [logic gates](https://en.wikipedia.org/wiki/Logic_gate), the binary system is used by almost all modern [computers and computer-based devices](https://en.wikipedia.org/wiki/Computer), as a preferred system of use, over various other human techniques of communication, because of the simplicity of the language and the noise immunity in physical implementation.

## Convert in bash

### Convert from binary

Bash can handle hexadecimal numbers if they are written as [arithmetic expansions](https://www.gnu.org/software/bash/manual/bash.html#Arithmetic-Expansion):

```bash
┌──(kali㉿kali)-[~]
└─$ echo "$((2#1100101011111110))" 
51966
```

### Convert from binary to ASCII

We can use the above method together with `xxd` to convert to ASCII-characters. This requires the binary string is space-separated per byte!

```bash
leviathan4@gibson:~/.trash$ binary_pw="00110000 01100100 01111001 01111000 01010100 00110111 01000110 00110100 01010001 01000100 00001010"
leviathan4@gibson:~/.trash$ for a in $binary_pw; do printf "%x" $((2#$a)); done | xxd -r -p; echo
0dyxT7F4QD
```

If this is not the case and the binary string is continuous, this approach can be used.

```bash
fold -w8 flag.txt | while read -r b; do printf '%02x' "$((2#$b))"; done | xxd -r -p
```

Example:

```bash
mission14@linuxagency:~$ cat flag.txt 
01101101011010010111001101110011011010010110111101101110001100010011010101111011011001100110001100110100001110010011000100110101011001000011100000110001001110000110001001100110011000010110010101100110011001100011000000110001001100010011100000110101011000110011001100110101001101000011011101100110001100100011010100110101001110010011011001111101
mission14@linuxagency:~$ fold -w8 flag.txt | while read -r b; do printf '%02x' "$((2#$b))"; done | xxd -r -p ;echo
mission15{fc4915d818bfaeff01185c3547f25596}
mission14@linuxagency:~$ 
```

### Convert to binary

You can convert to binary with`bc`

```bash
┌──(kali㉿kali)-[~]
└─$ echo "obase=2; 51966" | bc 
1100101011111110
```

With `bc` you can also convert from hex to binary as follows:

```bash
┌──(kali㉿kali)-[~]
└─$ echo "obase=2; ibase=16; cafe" | bc  
0

┌──(kali㉿kali)-[~]
└─$ echo "obase=2; ibase=16; CAFE" | bc 
1100101011111110
```

{% hint style="info" %}
Note that the hexadecimal number must be written in CAPITALS.
{% endhint %}

## Convert with Binary Refinery

You can convert from and to binary with [pack](https://binref.github.io/#refinery.pack) from Binary Refinery

```bash
┌──(kali㉿kali)-[~]
└─$ source ~/Python_venvs/Binary_Refinery/bin/activate

┌──(Binary_Refinery)─(kali㉿kali)-[~]
└─$ emit '00110000 01100100 01111001 01111000 01010100 00110111 01000110 00110100 01010001 01000100 00001010' | pack 2
0dyxT7F4QD

┌──(Binary_Refinery)─(kali㉿kali)-[~]
└─$ emit 'Test' | pack -R 2
1010100
1100101
1110011
1110100
```

Note that Binary Refinery assumes spaces between the binary bytes!

A long binary string without spaces can also be decoded though with `pack -B8 -E 2`

```bash
┌──(Binary_Refinery)─(kali㉿kali)-[/mnt/…/Huntress_CTF/Huntress_CTF_2025/Forensics/Bussing_Around]
└─$ echo '0101010001101000011001010010000001110000011000010111001101110011011101110110111101110010011001000010000001101001011100110010000000110101001110010011001100111001011001100011001101100101011000110011100101100100001110000011001000110000011001100011001000110011011001000110011000110010001100000011100100110100001110000110000101100110001100000011100101100001001101010011011000111000001100100010000000101110' | pack -B8 -E 2
The password is 5939f3ec9d820f23df20948af09a5682 .
```

## Convert in Python

Python can handle binary numbers out-of-the-box and they should be prefixed with `0b`.

### Convert from binary

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ python
Python 3.11.9 (main, Apr 10 2024, 13:16:36) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 0b1100101011111110
51966
>>> exit()
```

### Convert from binary string to ASCII

```python
#!/usr/bin/python

bin_string = '01100111011011110110111101100100001000000111011101101111011100100110101100100001'

# Divide the binary string into array of 8-bit binary string chunks
n = 8
split_array = [bin_string[i:i+n] for i in range(0, len(bin_string), n)]

# Convert to ascii text
flag = ""
for item in split_array:
    flag += chr(int(item, 2))
print(flag)

```

### Convert to binary

Use the `bin()`-function to convert to hexadecimal.

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ python
Python 3.11.9 (main, Apr 10 2024, 13:16:36) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> bin(51966)
'0b1100101011111110'
>>> bin(51966)[2:]
'1100101011111110'
>>> exit()
```

## Convert with online services

You can also convert binary numbers to ASCII with one of these online services:

branah.com: <https://www.branah.com/ascii-converter>

CyberChef: <https://gchq.github.io/CyberChef/#recipe=From_Binary('Space',8)>

RapidTables: <https://www.rapidtables.com/convert/number/ascii-hex-bin-dec-converter.html>

## Resources

Binary number - Wikipedia: <https://en.wikipedia.org/wiki/Binary_number>

Binary Refinery - Documentation: <https://binref.github.io/>

Binary Refinery - GitHub: <https://github.com/binref/refinery/>
