ASCII


ASCII Table


Each character in the ASCII table can be represented in 7 bits. For example, the null terminator is represented as 000 0000

The leftmost 8th bit is reserved for Unicode, which will label it as a 0 if the character is within the ASCII table

ASCII reserves the first 32 characters (0-31) as control characters, which are characters not intended to represent printable information.

Control characters refer to things a computer must process, such as an alert, a tab, or a null terminator. All of the control characters begin with 000. For instance, a horizontal tab is represented as 000 1001

The Racket with Brackets

After diving down a Wikipedia rabbit hole, I finally figured out what to call all of the punctuation used when coding. Brace yourselves (ha.)

Although by default, "bracket" refers to [these] brackets, every type of bracket has a name, and a descriptor, as shown in the table below:

Spoken NameTechnical NameCharacter
BracketSquare bracket[ ]
BraceCurly bracket{ }
ParenthesisRound bracket( )
ChevronAngle bracket< >

Modifier Keys

The precedence of modifier keys

  1. fn

ANSI Colors

ANSI escape codes are a standard for in-band signaling to control the cursor location, color, and other options on text terminals. The ANSI x3.64 standard was released in 1979 and later updated by the 5th edition ECMA-48 standard in 1991, ISO/IEC 6429 in 1992, and ISO-8613-3 in 1995. ANSI escape codes are started using \e[ and contain numeric codes separated by semicolons. The escape code is terminated with an 'm'. ANSI Color Codes.

ANSI Color Escape Sequences

CodeEffectNote
\x1b[CSIControl Sequence Indicator
CSI n mANSICOLORANSI color code (Select Graphic Rendition)
CSI 38 ; 5 ; n m256COLORForeground 8-bit 256 color code
CSI 48 ; 5 ; n m256COLORBackground 8-bit 256 color code
CSI 38 ; 2 ; r ; g ; b mTRUECOLORForeground 24-bit RGB color code
CSI 48 ; 2 ; r ; g ; b mTRUECOLORBackground 24-bit RGB color code

The 16 ANSI Colors

ColorCodeHex ValueCodeHex Value
Black0#0000008#555555
Red1#aa00009#ff5555
Green2#00aa0010#55ff55
Yellow3#aaaa0011#ffff55
Blue4#0000aa12#5555ff
Magenta5#aa00aa13#ff55ff
Cyan6#00aaaa14#55ffff
White7#aaaaaa15#ffffff

The xterm 256 colors

In 1999, Thomas Dickey, an xterm maintainer, using a patch created by Todd Larason to add support for 256 colors. These colors form a compatible extension to the OG 16 ANSI colors and has been adopted by most terminal emulators, including the macOS Terminal application, which runs on xterm, as well as MUD clients.

To send a 256 color foreground color one must print \e[38;5;COLORm where COLOR is a number between 0 and 255.

For background colors, it's pretty much the same, but the escape sequence starts with 48, instead of 38. Therefore, one must print \e[48;5;COLORm where COLOR is a number between 0 and 255.

NameCodeDimCodeBold
Azure0250066bb0330088ff
Blue0190000bb0210000ff
Cyan03700bbbb05100ffff
Ebony016000000059666666
Green03400bb0004600ff00
Jade03500bb6604800ff88
Lime07066bb0011888ff00
Magenta127bb00bb201ff00ff
Orange130bb6600208ff8800
Pink125bb0066198ff0088
Red124bb0000196ff0000
Silver102888888188dddddd
Tan094886600178ddbb00
Violet0556600bb0938800ff
White145bbbbbb231ffffff
Yellow142bbbb00226ffff00

50 24 shades of gray

(Couldn't help it.) To display code 232 through 255 using 24 bit RGB colors the following hexadecimal values are suggested for the 24 shade grayscale: start out at 8 for the first color and increment the value by 10 for each increment. \x08, \x12, \x1C, \x26, etc.

Select Graphic Rendition (SGR)

SGR Color Codes are used to format output on terminals, as well as in many coding languages, such as java. An SGR color code can be expressed by typing the escape character 0x1b followed by [ then the code number (e.g. 42) and then the letter m. When you put that all together, it looks like \x1b[42m.The formatting will persist until the reset code is given, which is code #0. You can specify this with \x1b[0m

echo -e -n '\x1b[42m Green \x1b[0m\n'

Here are some of the most useful codes:

Font Style

Escape CodeFunction
\x1b[0mreset all fonts, formats, colors, etc.
\x1b[1menable bold font
\x1b[2menable faded font
\x1b[3menable italic font
\x1b[4menable underlined font
\x1b[5menable blinking font
\x1b[7menable inverted-font
\x1b[22mdisable bold font
\x1b[23mdisable italic font
\x1b[24mdisable underlined font
\x1b[25mdisable blinking font
\x1b[27mdisable inverted-font

Foreground Color

ColorForegroundBackground
black\x1b[30m\x1b[40m
red\x1b[31m\x1b[41m
green\x1b[32m\x1b[42m
yellow\x1b[33m\x1b[43m
blue\x1b[34m\x1b[44m
magenta\x1b[35m\x1b[45m
cyan\x1b[36m\x1b[46m
white\x1b[37m\x1b[47m
select 256-color\x1b[38;5;<0-255>\x1b[48;5;<0-255>
default\x1b[39m\x1b[49m

The 256 colors of xterm

The first 16 colors have RGB tuples predetermined by ANSI that don't follow the same formula as the others

typeset -A rgb256

let index=0
# First two rows, colors [0..15]
for ((row = 0; row <= 1; row += 1)); do
    for ((blue = 0; blue <= 1; blue += 1)); do
        for ((green = 0; green <= 1; green += 1)); do
            for ((red = 0; red <= 1; red += 1)); do

            done
        done
    done
done
# Second row, colors [8..15]

predeter

XColorTable() {
  i=16
  for ((r = 0; r <= 255; r+=40)); do
    for ((g = 0; g <= 255; g+=40)); do
      for ((b = 0; b <= 255; b+=40)); do
        echo "Color$((i++)) = (${r}, ${g}, ${b})"
        if ((b == 0)); then
            b=55;
        fi
      done
      if ((b == 0)); then
        g=55;
      fi
    done
    if ((r == 0)); then
        r=55;
    fi
  done
  for ((m = 8; m <= 238; m+=10)); do # Do Monochrome
    echo "Color$((i++)) = (${m}, ${m}, ${m})"
  done
}

SGR Cursor Movements

Cursor/Screen Color

Escape CodeFunction
\EcReset screen to initial state

Modifying Content

Escape CodeFunction
\E[J / \E[0JClear from cursor to end of screen
\E[1JClear from cursor to beginning of stream
\E[2JClear entire screen
\E[3JClear entire screen and delete all lines saved in the scrollback buffer
\E[K / \E[0KErase from cursor to end of line
\E[1KErase start-of-line up until the cursor
\E[2KErase the entire line
\E[2XErase 2 characters (replaced with whitespace)
\E[2PDelete 2 characters (left-shift the text that follows)
\E[2MDelete 2 lines
\E[2@Insert 2 spaces (right-shift the text that follows)
\E[2LInsert 2 lines (right-shift the text that follows)

Modify Screen

Escape CodeFunction
\E[2SScroll up 2 lines
\E[2TScroll down 2 lines

Cursor Movement

Escape CodeFunction
\E[2AShift cursor up by 2 rows
\E[2BShift cursor down 2 rows
\E[2CShift cursor right 2 columns
\E[2DShift cursor left 2 columns
\E[2EShift cursor to the 1st column, 2 rows below
\E[2FShift cursor to the 1st column, 2 rows above
\E[2GMove cursor to column 2 (current row)
\E[2dMove cursor to row 2 (current column)
\E[i;jHMove cursor to row i, column j

Misc.

Escape CodeFunction
\E[nbRepeat previous character n times
\E7Save cursor position
\E8Restore cursor position

+3 +2 +1

Unicode

Unicode Transformation Format (UTF)

Unicode Transformation Format (UTF) is one of the mapping methods engineered to encode text. It does this by mapping code points to code values. Each code value is a unique sequence of bytes.

UTF-16

The UTF-16 encoding system, is not as simple as it's name suggests. Each char is not encoded with 16 bits, as is commonly assumed. UTF-16 is a variable-width encoding format.

Java's char object is encoded using UTF-16 and so are Windows filenames, as well as the C++ RESTful API SDK written my Microsoft, as well as the macOS operating system.

It's rarely advantageous to use UTF-16 over UTF-8. The only time it will result in a smaller file size is if the majority of text in the file consists of Chinese or Japanese characters. Even so, if there is a large amount of whitespace (which is an ASCII character) then the UTF-8 encoding would still result in a smaller file size. The standard norm is increasingly becoming UTF-8, and the trend shows no sign of slowing down.

UTF-32

UTF-32, unlike its brothers, is a fixed-width encoding format. Every character is guaranteed to be represented by exactly 4 bytes. UTF-32 is rarely used. Requiring every character to be represented with 4 bytes results in a significant increase in file size. It is slightly faster to read than UTF-8 but the difference is barely measurable.

Lastly, UTF-32 is problematic because it results in encoding many 8-bit strings of 0's. Traditional software interprets this as the null terminator, which signals the end of the string, which would truncate the remaining information previously encoded by UTF-32.