> 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/hexadecimal-numbers.md).

# Hexadecimal numbers

In [mathematics](https://en.wikipedia.org/wiki/Mathematics) and [computing](https://en.wikipedia.org/wiki/Computing), the hexadecimal (also base-16 or simply hex) numeral system is a [positional numeral system](https://en.wikipedia.org/wiki/Numeral_system#Positional_systems_in_detail) that represents numbers using a [radix](https://en.wikipedia.org/wiki/Radix) (base) of sixteen. Unlike the [decimal](https://en.wikipedia.org/wiki/Decimal) system representing numbers using ten symbols, hexadecimal uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values 0 to 9 and "A"–"F" (or "a"–"f") to represent values from ten to fifteen.

Software developers and system designers widely use hexadecimal numbers because they provide a convenient representation of [binary-coded](https://en.wikipedia.org/wiki/Binary_code) values. Each hexadecimal digit represents four [bits](https://en.wikipedia.org/wiki/Bit) (binary digits), also known as a [nibble](https://en.wikipedia.org/wiki/Nibble) (or nybble). For example, an 8-bit [byte](https://en.wikipedia.org/wiki/Byte) can have values ranging from 00000000 to 11111111 (0 to 255 decimal) in binary form, which can be written as 00 to FF in hexadecimal.

In mathematics, a subscript is typically used to specify the base. For example, the decimal value 42,260 would be expressed in hexadecimal as A51416. In programming, several notations denote hexadecimal numbers, usually involving a prefix. The prefix `0x` is used in [C](https://en.wikipedia.org/wiki/C_\(programming_language\)), which would denote this value as `0xA514`.

## Convert in bash

### Convert with xxd

You can convert hexadecimal numbers to ASCII with `xxd`

```bash
┌──(kali㉿kali)-[~]
└─$ echo '68 65 78 61 64 65 63 69 6d 61 6c 20 6f 72 20 62 61 73 65 31 36 3f' | xxd -r -p 
hexadecimal or base16? 
```

And to convert from ASCII to hexadecimal numbers

```bash
┌──(kali㉿kali)-[~]
└─$ echo -n 'test' | xxd -p
74657374
```

### Convert from hexadecimal to decimal numbers

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)-[~/Desktop]
└─$ echo "$((16#CAFE))" 

51966

┌──(kali㉿kali)-[~/Desktop]
└─$ echo "$((16#cafe))"

51966
```

### Convert numbers to hexadecimal

You can convert to hexadecimal with`bc`

```bash
┌──(kali㉿kali)-[~/Desktop]
└─$ echo "obase=16; 51966" | bc    
CAFE
```

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

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

Note that the order of the operators are important! `obase` should always be specified first.

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

## Convert with Binary Refinery

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

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

┌──(Binary_Refinery)─(kali㉿kali)-[~]
└─$ emit '68 65 78 61 64 65 63 69 6d 61 6c 20 6f 72 20 62 61 73 65 31 36 3f' | hex
hexadecimal or base16?

┌──(Binary_Refinery)─(kali㉿kali)-[~]
└─$ emit 'Test' | hex -R
54657374
```

## Convert in PowerShell

PowerShell understands the `0x`-prefix so to convert a hexadecimal number to decimal you just enter the number prefixed with `0x`

```powershell
PS C:\Users\Admin> 0x16E3000
23998464
```

Converting decimal numbers to hexadecimal can be done in many ways including:

```powershell
PS C:\Users\Admin> (23998464).ToString("X")
16E3000
PS C:\Users\Admin> (23998464).ToString("x")
16e3000
PS C:\Users\Admin> '{0:X}' -f 23998464
16E3000
PS C:\Users\Admin> [System.Convert]::ToString(23998464,16)
16e3000
```

## Convert in Python

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

### Convert from hexadecimal to decimal numbers

```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.
>>> 0xCAFE
51966
>>> 0xcafe
51966
>>> exit()
```

### Convert decimal numbers to hexadecimal

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

```python
┌──(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.
>>> hex(51966)
'0xcafe'
>>> hex(51966)[2:]
'cafe'
>>> exit()
```

If you need to mind the [endianess](https://en.wikipedia.org/wiki/Endianess) you can use [int.to\_bytes](https://docs.python.org/3/library/stdtypes.html#int.to_bytes)

```python
┌──(kali㉿kali)-[~]
└─$ python 
Python 3.13.2 (main, Feb  5 2025, 01:23:35) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print((1919831931).to_bytes(4, 'little')
... )
b'{Onr'
>>> print((1919831931).to_bytes(4, 'little'))
b'{Onr'
>>> print((1919831931).to_bytes(4, 'little').hex())
7b4f6e72
>>> print((1919831931).to_bytes(4, 'big'))
b'rnO{'
>>> print((1919831931).to_bytes(4, 'big').hex())
726e4f7b
>>> exit()
```

### Convert hex string to ASCII

```bash
┌──(kali㉿kali)-[~]
└─$ 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.
>>> print(bytearray.fromhex('68 65 78 61 64 65 63 69 6d 61 6c 20 6f 72 20 62 61 73 65 31 36 3f').decode())
hexadecimal or base16?
>>> exit()
```

## Convert with online services

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

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

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

## Resources

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

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

Hexadecimal - Wikipedia: <https://en.wikipedia.org/wiki/Hexadecimal>
