ASCII control characters

Revision as of 12:02, 7 April 2023 by Jjuanhdez (talk | contribs) (Added FreeBasic)

ASCII is the American Standard Code for Information Interchange. There are 128 ASCII characters.

C

enum: char {
nul,
soh,
stx,
etx,
eot,
enq,
ack,
bel,
bs,
ht,
lf,
vt,
ff,
cr,
so,
si,
dle,
dc1,
dc2,
dc3,
dc4,
nak,
syn,
etb,
can,
em,
sub,
esc,
fs,
gs,
rs,
us,
space,
del = 127
};

D

import std.ascii.ControlChar;

FreeBASIC

Enum AsciiControlChar
    NUL = &H00  'Null
    SOH = &H01  'Star of Header
    STX = &H02  'Start of Text
    ETX = &H03  'End of Text
    EOT = &H04  'End of Transmission
    ENQ = &H05  'Enquiry
    ACK = &H06  'Acknowledge
    BEL = &H07  'Bell
    BS = &H08   'BackSpace
    HT = &H09   'Horizontal Tabulation
    LF = &H0A   'Line Feed
    VT = &H0B   'Vertical Tabulation
    FF = &H0C   'Form Feed
    CR = &H0D   'Carriage Return
    SO = &H0E   'Shift Out
    SI = &H0F   'Shift In
    DLE = &H10  'Data Link Escape
    DC1 = &H11  'Device Control 1 (XON)
    DC2 = &H12  'Device Control 2
    DC3 = &H13  'Device Control 3 (XOFF)
    DC4 = &H14  'Device Control 4
    NAK = &H15  'Negative acknowledge
    SYN = &H16  'Synchronous Idle
    ETB = &H17  'End of Transmission Block
    CAN = &H18  'Cancel
    EM = &H19   'End of Medium
    SUB_ = &H1A 'Substitute
    ESC = &H1B  'Escape
    FS = &H1C   'File Separator
    GS = &H1D   'Group Separator
    RS = &H1E   'Record Separator
    US = &H1F   'Unit Separator
    SP = &H20   'Space
    DEL = &H7F  'Delete
End Enum

Print(Hex(AsciiControlChar.CR))
Print(Hex(AsciiControlChar.DEL))
Sleep
Output:
 D
7F

perl

use charnames ":loose";
# There is no EM, use END OF MEDIUM.
# Do not confuse BEL with BELL. Starting in Perl 5.18, BELL refers to unicode emoji 0x1F514. ALERT is an alias for BEL.
# compile time literal
"\N{nul}\N{soh}\N{stx}\N{etx}\N{eot}\N{enq}\N{ack}\N{bel}\N{bs}\N{ht}\N{lf}\N{vt}\N{ff}\N{cr}\N{so}\N{si}\N{dle}\N{dc1}\N{dc2}\N{dc3}\N{dc4}\N{nak}\N{syn}\N{etb}\N{can}\N{end of medium}\N{sub}\N{esc}\N{fs}\N{gs}\N{rs}\N{us}\N{space}\N{delete}"
# run time
charnames::string_vianame $_;

Wren

Library: Wren-dynamic

I assume this isn't intended to be a task but simply a reference to how individual languages might treat ASCII control characters en bloc using enum like structures or otherwise. Note that technically 'space' is a printable character, not a control character.

Wren doesn't have enums built into the language but can create them dynamically at runtime. However, such enums need to have consecutive integer values.

Here, we create instead a Group which can contain any values in any order.

import "./dynamic" for Group

var names = [
    "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
    "bs",  "ht",  "lf",  "vt",  "ff",  "cr",  "so",  "si", 
    "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
    "can", "em",  "sub", "esc", "fs",  "gs",  "rs",  "us",
    "space", "del"
]

var values = (0..32).toList + [127]

var Ctrl = Group.create("Ctrl", names, values)

// print some specimen values
System.print(Ctrl.cr)
System.print(Ctrl.del)
Output:
13
127