ASCII control characters

From Rosetta Code
Revision as of 11:51, 7 April 2023 by PureFox (talk | contribs) (→‎{{Header|C}}: Inserted missing 'ff' (form feed) character.)

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;

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{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