Numbers Systems

5 minute read

Computers uses several number system to represent and work with numbers.The most popular ones are binary, octal, decimal and hexadecimal.

Binary

Its made up of only 0’s and 1’s. This represent on and off state which is the only language a computer can understand as it is the easiest way to vary instructions through electric signals.

A Byte [01001010] is equivalence of 8bits
ASCII which stands for American Starndard code for information insterchange —-> Uses 7 bits

Binary Number == Base 2 There is 2 possible values 0 and 1
Octal No == Base 8 8 digits 0-7
Decimal No == Base 10 There are 10 digits 0-9
Hexadecimal No == Base 16 There 16 digits 0-9 ABCDEF

Hexadecimal: [ 0 1 2 3 4 5 6 7 8 9 A B C D E F ]
Decimal: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ]

1 bit Bit
4 bits Nibble
8 bits Byte
16 bits/2bytes Word
32 bits/4bytes Long word
64 bits/8bytes Very Log word

ASCII (American Standard Code for Information Interchange) is a code for representing 128 English characters as numbers, with each letter assigned a number from 0 to 127.

Base64 Encoding

When you have some binary data that you want to ship across a network, you generally don’t do it by just streaming the bits and bytes over the wire in a raw format. Why? because some media are made for streaming text. You never know – some protocols may interpret your binary data as control characters (like a modem), or your binary data could be screwed up because the underlying protocol might think that you’ve entered a special character combination (like how FTP translates line endings).

To get around this the binary data needs to be encoded to characters. Base64 works by taking 3 bytes from a string and returning 4bytes

Take the word “Hey”. The Ascii representation of each letter in the above word will be:
72 [H]
101 [e]
121 [y]

The binary representation of the Ascii decimals will be:
72 [H] [01001000]
101 [e] [01100101]
121 [y] [01111001]

Base64 works by taking 3 bytes from a byte stream and return 4 bytes each made up of 6 bits.

[H]           [e]         [y]              
[72]           [101]         [121]             < - - - - Decimal representation
[01001000]     [01100101]     [01111001]           < - - - - 8bit representation
[010010]       [000110]       [010101]     [111001] < - - - - Regrouped to 6 bits - group
[18]       [6]       [21]     [57] < - - - - Decimal representation
[S]       [G]       [V]     [5] < - - - - Base64

Each of the newly formed digit is converted to a single character in the Base64 alphabet table made up of 65-character subset of the ASCII charset. The first 64 characters out of the 65-character subset are mapped to an equivalent 6-bit binary sequence. The extra 65th character (=) is used for padding.

When the input has fewer than 24 bits at the end, zero bits are added (on the right) to form an integral number of 6-bit groups. Then, one or two pad (=) characters are output depending on the following cases.

  • The last chunk of input contains exactly 8 bits: Four zero bits are added to form two 6-bit groups. Each 6-bit group is converted to the resulting Base64 encoded character using the Base64 index table. After that two pad (=) characters are appended to the output.

  • The last chunk of input contains exactly 16 bits: Two zero bits are added to form three 6-bit groups. Each of the three 6-bit groups is converted to the corresponding Base64 alphabet. Finally a single pad (=) character is appended to the output.

2^6 = 64

Python Code: For an input made up of exactly 3 bytes.

>>> import base64
>>> message = b"Hey"
>>> base64.b64encode(message)
b'SGV5'

Python Code: For an input made up of 4 bytes.

>>> import base64
>>> message = b"Hey."     // adde a period at the end of the word Hey.
>>> base64.b64encode(message)
b'SGV5Lg=='     // Outputs with double pad (==)

Python Code: For an input made up of 5 bytes (16 bits).

>>> import base64
>>> message = b"Heys."
>>> base64.b64encode(message)
b'SGV5cy4='    // Appends single pad(=)

Hexadecimal

Hexadecimal is a convenient way to express binary numbers in modern computers in which a byte is almost always defined as containing eight binary digits. When showing the contents of computer storage (for example, when getting a core dump of storage in order to debug a new computer program or when expressing a string of text characters or a string of binary values in coding a program or HTML page), one hexadecimal digit can represent the arrangement of four binary digits. Two hexadecimal digits can represent eight binary digits, or a byte.

0 1 2 3 4 5 6 7 8 9 A B C D E F

Decimal to Hexadecimal

65 ——————— Decimal
0 1 0 0 0 0 0 1 ————> Binary
128 64 32 16 8 4 2 1
0 1 0 0 0 0 0 1

Hexadecinal conversion

8 4 2 1 | 8 4 2 1
0 1 0 0 | 0 0 0 1
4           1

41 is the Hexadicimal value of 65, Also written as 0x41. The prefix 0x is a notation for Hexa as 0b is for Binary.

Binascii converts binary data to hex

>>> import binascii
>>> x = b'A'
>>> binascii.hexlify(x)
b'41'
>>> _.decode()
'41'
---------------------------------------------------------------------------------
>>> x = "Cyber Security".encode()
>>> binascii.hexlify(x)
b'4379626572205365637572697479'
>>> _.decode()
'4379626572205365637572697479'
---------------------------------------------------------------------------------
Decimal conversion:
>>> hex(65)
'0x41'
>>> _[2:]
'41'
>>> 

Converting hex values to ASCII text

>>> import binascii
>>> binascii.unhexlify('41').decode()
'A'
---------------------------------------------------------------------------------
>>> x = "Cyber Security".encode()
>>> binascii.hexlify(x).decode()
'4379626572205365637572697479'
>>> bytes.fromhex(_)
b'Cyber Security'
>>> _.decode()
'Cyber Security'

Converting hex to decimal.

>>> int('41', 16)
65

Convertion using command line tool:

ulab@ulab-ws:~$ echo "Hallo" | xxd -ps
48616c6c6f0a
---------------------------------------------------------------------------------
ulab@ulab-ws:~$ printf "48616c6c6f0a" | xxd -r -p
Hallo

Decimal to binary:

>>> x = 65
>>> bin(x)
'0b1000001'
---------------------------------------------------------------------------------
Reverse:
>>> x = bin(65)[2:]
>>> x
'1000001'
>>> int(x, 2)
65

Leave a comment