ASCIIとは?すべての始まりになったCharacter Encoding
入力するletter、number、symbolの裏側にはnumeric codeがあります。ASCIIはその物語の出発点です。computersにtextの共通言語を与えたencoding standardです。
目次
ASCIIとは?
ASCIIはAmerican Standard Code for Information Interchangeの略です。English textで使われるletters、digits、punctuation marks、control charactersに0から127のnumeric codeを割り当てるcharacter encoding standardです。
たとえばuppercaseのAは65、digitの0は48、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 01101001ASCIIを認識しやすく、扱いやすくする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です。
| Code | Char | Code | Char | Code | Char |
|---|---|---|---|---|---|
| 32 | Space | 48 | 0 | 64 | @ |
| 33 | ! | 49 | 1 | 65 | A |
| 34 | " | 50 | 2 | 66 | B |
| 35 | # | 51 | 3 | 67 | C |
| 36 | $ | 52 | 4 | 68 | D |
| 37 | % | 53 | 5 | 69 | E |
| 38 | & | 54 | 6 | 70 | F |
| 39 | ' | 55 | 7 | 71 | G |
| 40 | ( | 56 | 8 | 72 | H |
| 41 | ) | 57 | 9 | 73 | I |
| 42 | * | 58 | : | 74 | J |
| 43 | + | 59 | ; | 75 | K |
| 44 | , | 60 | < | 76 | L |
| 45 | - | 61 | = | 77 | M |
| 46 | . | 62 | > | 78 | N |
| 47 | / | 63 | ? | 79 | O |
| 80 | P | 96 | ` | 112 | p |
| 81 | Q | 97 | a | 113 | q |
| 82 | R | 98 | b | 114 | r |
| 83 | S | 99 | c | 115 | s |
| 84 | T | 100 | d | 116 | t |
| 85 | U | 101 | e | 117 | u |
| 86 | V | 102 | f | 118 | v |
| 87 | W | 103 | g | 119 | w |
| 88 | X | 104 | h | 120 | x |
| 89 | Y | 105 | i | 121 | y |
| 90 | Z | 106 | j | 122 | z |
| 91 | [ | 107 | k | 123 | { |
| 92 | \ | 108 | l | 124 | | |
| 93 | ] | 109 | m | 125 | } |
| 94 | ^ | 110 | n | 126 | ~ |
Control Characters (0-31)
最初の32個のASCII codesはcontrol charactersです。printable symbolsではなく、もともとはterminals、printers、modems、teletypesへのinstructionsでした。今も日常的に重要なものがあります。
| Code | Name | Abbreviation | Usage |
|---|---|---|---|
| 0 | Null | NUL | C/C++でのstring terminator |
| 9 | Horizontal Tab | HT | tab character (\t) |
| 10 | Line Feed | LF | Unix/Linux/macOSのnew line (\n) |
| 13 | Carriage Return | CR | WindowsでLFと一緒に使われる (\r\n) |
| 27 | Escape | ESC | colorsやcursor movement用のterminal escape sequences |
| 127 | Delete | DEL | もともとは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に整理できます。
| Range | Codes | Description |
|---|---|---|
| Space | 32 | space character |
| Punctuation/Symbols | 33-47, 58-64, 91-96, 123-126 | ! " # $ % & ' ( ) * + , - . / : ; など |
| Digits | 48-57 | 0 1 2 3 4 5 6 7 8 9 |
| Uppercase Letters | 65-90 | A B C D E F ... Z |
| Lowercase Letters | 97-122 | a 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; // 3ASCII vs Unicode
ASCIIが扱えるのは128 charactersだけで、basic English textには十分ですが、世界中のtextには足りません。そこで、より多くのscripts、symbols、emojisを表すためにUnicodeが作られました。
| Feature | ASCII | Unicode (UTF-8) |
|---|---|---|
| Characters | 128 | 149,000+ |
| Languages | English only | All modern scripts |
| Bits per character | 7 (stored as 8) | 8-32, variable length |
| Emoji support | No | Yes |
| 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を試す参考資料
- ANSI. (1986). ANSI X3.4-1986 - Coded Character Sets - 7-Bit American National Standard Code for Information Interchange. https://www.ansi.org
- Cerf, V. (1969). ASCII format for Network Interchange. RFC 20, IETF. https://datatracker.ietf.org/doc/html/rfc20
- Mozilla Developer Network. String.prototype.charCodeAt() - JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
- The Unicode Consortium. The Unicode Standard. https://www.unicode.org/standard/standard.html
- Wikipedia. ASCII - History and Development. https://en.wikipedia.org/wiki/ASCII