/8分で読めます

ASCIIとは?すべての始まりになったCharacter Encoding

入力するletter、number、symbolの裏側にはnumeric codeがあります。ASCIIはその物語の出発点です。computersにtextの共通言語を与えたencoding standardです。

ASCIIとは?

ASCIIAmerican Standard Code for Information Interchangeの略です。English textで使われるletters、digits、punctuation marks、control charactersに0から127のnumeric codeを割り当てるcharacter encoding standardです。

たとえばuppercaseのA65、digitの048、spaceは32で表されます。このmappingにより、computersはtextをnumbersのsequenceとして保存・送信できます。

ASCIIはcharacterごとに7 bitsを使い、128 possible valuesを持ちます。uppercase/lowercaseのEnglish alphabet、digits 0-9、common punctuation、少数のcontrol commandsには十分でした。

ASCIIの簡単な歴史

ASCIIは1960年代初頭にAmerican Standards Association、現在のANSIのcommitteeによって開発されました。standardとして最初に公開されたのは1963年で、最後の更新は1986年のANSI X3.4-1986です。

ASCII以前は、computerやcommunication systemsが互換性のないcharacter codesを使うことがよくありました。IBM systemsはEBCDIC、telegraph systemsはBaudot codeを使い、machines間でtextを交換するのは必要以上に難しい作業でした。

ASCIIはcomputers、terminals、printers、networksに共通のtext vocabularyを作りました。carriage returnやline feedのようなcontrol charactersの多くは、teletype machinesなど当時のmechanical devicesに由来します。

ASCIIの仕組み

ASCIIは各characterを7-bit binary numberへmapします。modern computersでは通常、これらの値を8-bit bytesに保存し、extra bitは0にするか、historicallyにはparity checkingに使われました。

Character -> Decimal -> Binary

  'A'  ->  65  ->  01000001
  'B'  ->  66  ->  01000010
  'a'  ->  97  ->  01100001
  '0'  ->  48  ->  00110000
  ' '  ->  32  ->  00100000
  '!'  ->  33  ->  00100001

The word "Hi" is stored as:
  H = 72, i = 105
  Binary: 01001000 01101001

ASCIIを認識しやすく、扱いやすくするpatternsがいくつかあります。

  • Uppercase letters A-Zはcodes 65-90を使います。
  • Lowercase letters a-zはcodes 97-122を使います。
  • uppercaseとlowercaseの差は常に32で、これは便利なbit differenceでもあります。
  • Digits 0-9はcodes 48-57を使うため、digitのnumeric valueはcodeから48を引くと得られます。

ASCII Table

主要なprintable ASCII charactersは次の通りです。最初のprintable valueは32のspace characterで、最後は126のtildeです。

CodeCharCodeCharCodeChar
32Space48064@
33!49165A
34"50266B
35#51367C
36$52468D
37%53569E
38&54670F
39'55771G
40(56872H
41)57973I
42*58:74J
43+59;75K
44,60<76L
45-61=77M
46.62>78N
47/63?79O
80P96`112p
81Q97a113q
82R98b114r
83S99c115s
84T100d116t
85U101e117u
86V102f118v
87W103g119w
88X104h120x
89Y105i121y
90Z106j122z
91[107k123{
92\108l124|
93]109m125}
94^110n126~

Control Characters (0-31)

最初の32個のASCII codesはcontrol charactersです。printable symbolsではなく、もともとはterminals、printers、modems、teletypesへのinstructionsでした。今も日常的に重要なものがあります。

CodeNameAbbreviationUsage
0NullNULC/C++でのstring terminator
9Horizontal TabHTtab character (\t)
10Line FeedLFUnix/Linux/macOSのnew line (\n)
13Carriage ReturnCRWindowsでLFと一緒に使われる (\r\n)
27EscapeESCcolorsやcursor movement用のterminal escape sequences
127DeleteDELもともとはpunched tape charactersを消すために使われました

operating systems間のline endingの違い、Unix-like systemsの\nとWindowsの\r\nは、ASCII control charactersの直接のlegacyです。

Printable Characters (32-126)

95個のprintable ASCII charactersは、logical rangesに整理できます。

RangeCodesDescription
Space32space character
Punctuation/Symbols33-47, 58-64, 91-96, 123-126! " # $ % & ' ( ) * + , - . / : ; など
Digits48-570 1 2 3 4 5 6 7 8 9
Uppercase Letters65-90A B C D E F ... Z
Lowercase Letters97-122a b c d e f ... z

CodeでのASCII

多くのprogramming languagesでは、charactersとASCII codesを簡単に相互変換できます。

JavaScript

// Character to ASCII code
"A".charCodeAt(0);  // 65
"a".charCodeAt(0);  // 97
" ".charCodeAt(0);  // 32

// ASCII code to character
String.fromCharCode(65);   // "A"
String.fromCharCode(72, 101, 108, 108, 111);  // "Hello"

// Convert case using ASCII math
const lower = String.fromCharCode("A".charCodeAt(0) + 32); // "a"
const upper = String.fromCharCode("a".charCodeAt(0) - 32); // "A"

Python

# Character to ASCII code
ord('A')   # 65
ord('a')   # 97

# ASCII code to character
chr(65)    # 'A'
chr(97)    # 'a'

# Convert a string to ASCII codes
[ord(c) for c in "Hello"]  # [72, 101, 108, 108, 111]

Practical Tricks

// Check if a character is a digit
const isDigit = (c) => c.charCodeAt(0) >= 48 && c.charCodeAt(0) <= 57;

// Check if a character is a letter
const isLetter = (c) => {
  const code = c.charCodeAt(0);
  return (code >= 65 && code <= 90) || (code >= 97 && code <= 122);
};

// Get alphabet position (A=1, B=2, ...)
const position = "C".charCodeAt(0) - 64;  // 3

ASCII vs Unicode

ASCIIが扱えるのは128 charactersだけで、basic English textには十分ですが、世界中のtextには足りません。そこで、より多くのscripts、symbols、emojisを表すためにUnicodeが作られました。

FeatureASCIIUnicode (UTF-8)
Characters128149,000+
LanguagesEnglish onlyAll modern scripts
Bits per character7 (stored as 8)8-32, variable length
Emoji supportNoYes
Backward compatibility-Yes, first 128 code points match ASCII

重要なのは、UTF-8はASCIIのsupersetだという点です。valid ASCII textはそのままvalid UTF-8でもあります。だからUnicodeの時代でもASCIIは今なお重要です。

ASCIIが今も使われる場所

ASCIIは60年以上前のstandardですが、今でもあらゆる場所で使われています。

  • Programming languages - variable names、keywords、operators、syntaxの多くはASCII charactersです。
  • Network protocols - HTTP headers、SMTP commands、FTP、多くの古いprotocolsはASCII-basedです。
  • File formats - CSV、JSON、HTML、XML、Markdown、INI filesはASCII-compatible textの上に成り立っています。
  • URLs - domain namesや多くのURL componentsはASCII-firstで、その他のcharactersはencodedされます。
  • Terminals and command lines - shell commands、environment variables、多くのfile pathsはASCII-compatible textに依存しています。
  • Embedded systems - 小さなdevicesでは、simple、compact、predictableなASCIIがよく使われます。
  • ASCII art - diagramsやtext-based visualsは、documentation、logs、retro computing cultureで今もよく見られます。

よくある質問

ASCIIはcase-sensitiveですか?

はい。uppercaseとlowercase lettersは別のcodesを持ちます。"A"は65、"a"は97です。差は常に32です。

Extended ASCIIとは何ですか?

Extended ASCIIは通常、8-bit byte全体、codes 128-255をextra symbolsに使うことを指します。ただしuniversalなextended ASCIIは1つではありません。Latin-1、Windows-1252、その他のencodingsはこの範囲を異なる方法で使います。

なぜcontrol charactersがあるのですか?

teletypes、printers、modemsのようなhardware向けに設計されたためです。BELはbellを鳴らし、CRはprint headを戻し、LFはpaperを進めました。TAB、LF、CR、ESCなどは今も使われています。

ASCIIでemojiやEnglish以外のcharactersを表せますか?

いいえ。ASCIIはbasic English-oriented charactersの128個だけを扱います。Thai、Chinese、Arabic、emoji、ほとんどのmodern textにはUnicodeが必要で、通常はUTF-8でencodedします。

TextをASCII Codesへ変換

無料のASCII Converter toolで、textをASCII codesへ変換したり、ASCII valuesをtextへdecodeしたりできます。

ASCII Converterを試す

参考資料

  1. ANSI. (1986). ANSI X3.4-1986 - Coded Character Sets - 7-Bit American National Standard Code for Information Interchange. https://www.ansi.org
  2. Cerf, V. (1969). ASCII format for Network Interchange. RFC 20, IETF. https://datatracker.ietf.org/doc/html/rfc20
  3. Mozilla Developer Network. String.prototype.charCodeAt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
  4. The Unicode Consortium. The Unicode Standard. https://www.unicode.org/standard/standard.html
  5. Wikipedia. ASCII - History and Development. https://en.wikipedia.org/wiki/ASCII