2.1 Encoding Formats

Week 2 Day 1 - Ciphers and Public/Private Key

Last Update Unknown

Binary and Hexadecimal

Hexadecimal

Hexadecimal numbers are a base-16 system as there are 16 digits. The first ten digits are 0 to 9, and the remaining 6 digits are represented by the letters A, B, C, D, E and F.

Hexadecimal is used as a short hand for writing out long binary numbers. It is much easier for humans to read and understand than binary!


We use the same systems as before, except the columns are now worth 1, 16, 16^2 (256), 16^3 (4096) and so on.


Convert Binary to Hexadecimal

Group binary digits into groups of 4, then look for the corresponding pattern

Example:

Convert the binary value 00111010 to hexadecimal

0011 1010

3 A


Convert the binary value 1001001001111111 to hexadecimal

1001 0010 0111 1111

9 2 7 F


Octal

Octal numbers are a base-8 system as there are 8 digits (0 to 7).

Example:

Convert the octal number 236 to decimal

236₈ = 2x8² + 3x8¹ + 6x8⁰ = 128 + 24 + 6 = 158


ASCII

ASCII (American Standard Code for Information Interchange) is a system which maps certain numbers to characters or other functions related with text processing.

ASCII uses 7 bits. However, there is also Extended ASCII which uses 8 bits and can represent more characters.


  • ASCII 0 - 32 and 127 are non-printable characters
    • Space, CR, ACK, NAK, DEL
  • ASCII 32-126 are printable characters
    • Upper and lower case letters, symbols, numbers


ASCII is being replaced by UTF-8 but still has some uses with old technology or 'text-only' protocols like HTTP.


Base64

Base64 is a way to convert or encode all characters to printable ASCII characters only.

It is used when we want to display, store or transmit non-printable characters. An example of this is a binary file or images over HTTP or e-mail.


Example:

  • Character 'M' is ASCII 77 which in binary is 01001101
  • The first six binary digits (010011) are 19 in decimal
  • Index 19 of Base64 table is character 'T'
  • The last two binary digits from M will be grouped with the first digits of the next character, contributing to form 22 (W)

Base64 Padding

  • A complete Base64 block needs 24 bits. If that’s not true, the block is padded with one or two zeros to the end
  • Padding is indicated in the output with one or two = signs


  • Padding of one 6-bit ‘word’ is indicated with '=' like above


  • Padding of two 6-bit words is indicated with '==' like above


  • Whether it’s '=' or '==' depends on the kind of input rather than the length of input
  • 'Ma' in Base64 is 'TWE=' whilst 'M0' in Base64 is 'TD==' even though the inputs are of the same length

HTTP/HTML/URL

HTTP

  • HTTP is a text-only protocol yet it can transfer images
  • To do this, it uses Base64 to encode them to text-only first
  • The e-mail protocols (SMTP/POP/IMAP) are also similar

HTTP URL

  • HTTP URLs (locations) also need to be text-only
  • In this case, the non-printable characters are displayed with their ASCII value in Hex, with % as notation: %66

HTML Colours

  • HTML colours are displayed in RGB like this:
    • #FF0000 = red, #00FF00 = green, #0000FF = blue
    • Any intermediate combination is also possible, e.g. #7C32AF
    • These are Hex representations of RGB values, each 0 - 255

Bitwise Operations


Bit shifts

Left shift

A logical left shift shifts all of the bits in a binary string to the left by a specified number of places.


For example, a left shift by one place would involve:

  • Moving all of the bits in the string one place to the left
  • Discarding the most significant (leftmost) bit
  • Putting a 0,0 into the empty place on the right

Right shift

A logical right shift shifts all of the bits in a binary string to the right by a specified number of places.


For example, a right shift by one place would involve:

  • Moving all of the bits in the string one place to the right
  • Discarding the least significant (rightmost) bit
  • Putting a 0,0 into the empty place on the left

Cyclic shift

In a cyclic shift, no bits are lost. The bits that are shifted out at one end are moved into the other end of the register.