js-codepage
Codepages聽are character encodings. In many contexts, single- or double-byte character sets are used in lieu of Unicode encodings. The codepages map between characters and numbers.
Setup
In node:
varcptable=require('codepage');
In the browser:
Alternatively, use the full version in the dist folder:
The complete set of codepages is large due to some Double Byte Character Set encodings. A much smaller file that only includes SBCS codepages is provided in this repo (sbcs.js), as well as a file for other projects (cpexcel.js)
If you know which codepages you need, you can include individual scripts for each codepage. The individual files are provided in the聽bits/聽directory. For example, to include only the Mac codepages:
All of the browser scripts define and append to the聽cptable聽object. To rename the object, edit the聽JSVAR聽shell variable in聽make.sh聽and run the script.
The utilities functions are contained in聽cputils.js, which assumes that the appropriate codepage scripts were loaded.
The script will manipulate聽module.exports聽if available . This is not always desirable. To prevent the behavior, define聽DO_NOT_EXPORT_CODEPAGE.
Usage
Most codepages are indexed by number. To get the Unicode character for a given codepoint, use the聽dec聽property:
varunicode_cp10000_255=cptable[10000].dec[255];// 藝
To get the codepoint for a given character, use the聽enc聽property:
varcp10000_711=cptable[10000].enc[String.fromCharCode(711)];// 255
There are a few utilities that deal with strings and buffers:
var姹囨��=cptable.utils.decode(936,[0xbb,0xe3,0xd7,0xdc]);varbuf=cptable.utils.encode(936,姹囨��);varsushi=cptable.utils.decode(65001,[0xf0,0x9f,0x8d,0xa3]);// 馃崳varsbuf=cptable.utils.encode(65001,sushi);
cptable.utils.encode(CP, data, ofmt)聽accepts a String or Array of characters and returns a representation controlled by聽ofmt:
Default output is a Buffer (or Array) of bytes (integers between 0 and 255)
If聽ofmt == 'str', return a binary String (byte聽i聽is聽o.charCodeAt(i))
If聽ofmt == 'arr', return an Array of bytes
cptable.utils.decode(CP, data)聽accepts a byte String or Array of numbers or Buffer and returns a JS string.
Known Excel Codepages
A much smaller script, including only the codepages known to be used in Excel, is available under the name聽cpexcel. It exposes the same variable聽cptable聽and is suitable as a drop-in replacement when the full codepage tables are not needed.
In node:
varcptable=require('codepage/dist/cpexcel.full');
Rolling your own script
The聽make.sh聽script in the repo can take a manifest and generate JS source.
Usage:
$ bash make.sh path_to_manifest output_file_name JSVAR
where
JSVAR聽is the name of the exported variable (generally聽cptable)
output_file_name聽is the output file (cpexcel.js,聽cptable.js, ...)
path_to_manifest聽is the path to the manifest file.
The manifest file is expected to be a CSV with 3 columns:
If a source is specified, it will try to download the specified file and parse. The file format is expected to follow the format from the unicode.org site. The size should be聽1聽for a single-byte codepage and聽2聽for a double-byte codepage. For mixed codepages (which use some single- and some double-byte codes), the script assumes the mapping is a prefix code and generates efficient JS code.
Generated scripts only include the mapping.聽cat聽a mapping with聽cputils.js聽to produce a complete script like聽cpexcel.full.js.
Building the complete script
This script uses聽voc. The script to build the codepage tables and the JS source is聽codepage.md, so building involves聽voc codepage.md.
Generated Codepages
The complete list of codepages can be found in the file聽pages.csv.
Some codepages are easier to implement algorithmically. Since those character tables are not generated, there is no corresponding entry (they are "magic").
CP#SourceDescription
37unicode.orgIBM EBCDIC US-Canada
437unicode.orgOEM United States
500unicode.orgIBM EBCDIC International
620NLSMazovia (Polish) MS-DOS
708Windows 7Arabic (ASMO 708)
720Windows 7Arabic (Transparent ASMO); Arabic (DOS)
737unicode.orgOEM Greek (formerly 437G); Greek (DOS)
775unicode.orgOEM Baltic; Baltic (DOS)
808unicode.orgOEM Russian; Cyrillic + Euro symbol
850unicode.orgOEM Multilingual Latin 1; Western European (DOS)
852unicode.orgOEM Latin 2; Central European (DOS)
855unicode.orgOEM Cyrillic (primarily Russian)
857unicode.orgOEM Turkish; Turkish (DOS)
858Windows 7OEM Multilingual Latin 1 + Euro symbol
860unicode.orgOEM Portuguese; Portuguese (DOS)
861unicode.orgOEM Icelandic; Icelandic (DOS)
862unicode.orgOEM Hebrew; Hebrew (DOS)
863unicode.orgOEM French Canadian; French Canadian (DOS)
864unicode.orgOEM Arabic; Arabic (864)
865unicode.orgOEM Nordic; Nordic (DOS)
866unicode.orgOEM Russian; Cyrillic (DOS)
869unicode.orgOEM Modern Greek; Greek, Modern (DOS)
870Windows 7IBM EBCDIC Multilingual/ROECE (Latin 2)
872unicode.orgOEM Cyrillic (primarily Russian) + Euro Symbol
874unicode.orgWindows Thai
875unicode.orgIBM EBCDIC Greek Modern
895NLSKamenick媒 (Czech) MS-DOS
932unicode.orgJapanese Shift-JIS
936unicode.orgSimplified Chinese GBK
949unicode.orgKorean
950unicode.orgTraditional Chinese Big5
1010IBMIBM EBCDIC French
1026unicode.orgIBM EBCDIC Turkish (Latin 5)
1047Windows 7IBM EBCDIC Latin 1/Open System
1132IBMIBM EBCDIC Lao (1132 / 1133 / 1341)
1140Windows 7IBM EBCDIC US-Canada (037 + Euro symbol)
1141Windows 7IBM EBCDIC Germany (20273 + Euro symbol)
1142Windows 7IBM EBCDIC Denmark-Norway (20277 + Euro symbol)
1143Windows 7IBM EBCDIC Finland-Sweden (20278 + Euro symbol)
1144Windows 7IBM EBCDIC Italy (20280 + Euro symbol)
1145Windows 7IBM EBCDIC Latin America-Spain (20284 + Euro symbol)
1146Windows 7IBM EBCDIC United Kingdom (20285 + Euro symbol)
1147Windows 7IBM EBCDIC France (20297 + Euro symbol)
1148Windows 7IBM EBCDIC International (500 + Euro symbol)
1149Windows 7IBM EBCDIC Icelandic (20871 + Euro symbol)
1200magicUnicode UTF-16, little endian (BMP of ISO 10646)
1201magicUnicode UTF-16, big endian
1250unicode.orgWindows Central Europe
1251unicode.orgWindows Cyrillic
1252unicode.orgWindows Latin I
1253unicode.orgWindows Greek
1254unicode.orgWindows Turkish
1255unicode.orgWindows Hebrew
1256unicode.orgWindows Arabic
1257unicode.orgWindows Baltic
1258unicode.orgWindows Vietnam
1361Windows 7Korean (Johab)
10000unicode.orgMAC Roman
10001Windows 7Japanese (Mac)
10002Windows 7MAC Traditional Chinese (Big5)
10003Windows 7Korean (Mac)
10004Windows 7Arabic (Mac)
10005Windows 7Hebrew (Mac)
10006unicode.orgGreek (Mac)
10007unicode.orgCyrillic (Mac)
10008Windows 7MAC Simplified Chinese (GB 2312)
10010Windows 7Romanian (Mac)
10017Windows 7Ukrainian (Mac)
10021Windows 7Thai (Mac)
10029unicode.orgMAC Latin 2 (Central European)
10079unicode.orgIcelandic (Mac)
10081unicode.orgTurkish (Mac)
10082Windows 7Croatian (Mac)
12000magicUnicode UTF-32, little endian byte order
12001magicUnicode UTF-32, big endian byte order
20000Windows 7CNS Taiwan (Chinese Traditional)
20001Windows 7TCA Taiwan
20002Windows 7ETEN Taiwan (Chinese Traditional)
20003Windows 7IBM5550 Taiwan
20004Windows 7TeleText Taiwan
20005Windows 7Wang Taiwan
20105Windows 7Western European IA5 (IRV International Alphabet 5)
20106Windows 7IA5 German (7-bit)
20107Windows 7IA5 Swedish (7-bit)
20108Windows 7IA5 Norwegian (7-bit)
20127magicUS-ASCII (7-bit)
20261Windows 7T.61
20269Windows 7ISO 6937 Non-Spacing Accent
20273Windows 7IBM EBCDIC Germany
20277Windows 7IBM EBCDIC Denmark-Norway
20278Windows 7IBM EBCDIC Finland-Sweden
20280Windows 7IBM EBCDIC Italy
20284Windows 7IBM EBCDIC Latin America-Spain
20285Windows 7IBM EBCDIC United Kingdom
20290Windows 7IBM EBCDIC Japanese Katakana Extended
20297Windows 7IBM EBCDIC France
20420Windows 7IBM EBCDIC Arabic
20423Windows 7IBM EBCDIC Greek
20424Windows 7IBM EBCDIC Hebrew
20833Windows 7IBM EBCDIC Korean Extended
20838Windows 7IBM EBCDIC Thai
20866Windows 7Russian Cyrillic (KOI8-R)
20871Windows 7IBM EBCDIC Icelandic
20880Windows 7IBM EBCDIC Cyrillic Russian
20905Windows 7IBM EBCDIC Turkish
20924Windows 7IBM EBCDIC Latin 1/Open System (1047 + Euro symbol)
20932Windows 7Japanese (JIS 0208-1990 and 0212-1990)
20936Windows 7Simplified Chinese (GB2312-80)
20949Windows 7Korean Wansung
21025Windows 7IBM EBCDIC Cyrillic Serbian-Bulgarian
21027NLSExtended/Ext Alpha Lowercase
21866Windows 7Ukrainian Cyrillic (KOI8-U)
28591unicode.orgISO 8859-1 Latin 1 (Western European)
28592unicode.orgISO 8859-2 Latin 2 (Central European)
28593unicode.orgISO 8859-3 Latin 3
28594unicode.orgISO 8859-4 Baltic
28595unicode.orgISO 8859-5 Cyrillic
28596unicode.orgISO 8859-6 Arabic
28597unicode.orgISO 8859-7 Greek
28598unicode.orgISO 8859-8 Hebrew (ISO-Visual)
28599unicode.orgISO 8859-9 Turkish
28600unicode.orgISO 8859-10 Latin 6
28601unicode.orgISO 8859-11 Latin (Thai)
28603unicode.orgISO 8859-13 Latin 7 (Estonian)
28604unicode.orgISO 8859-14 Latin 8 (Celtic)
28605unicode.orgISO 8859-15 Latin 9
28606unicode.orgISO 8859-15 Latin 10
29001Windows 7Europa 3
38598Windows 7ISO 8859-8 Hebrew (ISO-Logical)
47451unicode.orgAtari ST/TT
50220magicISO 2022 JIS Japanese with no halfwidth Katakana
50221magicISO 2022 JIS Japanese with halfwidth Katakana
50222magicISO 2022 Japanese JIS X 0201-1989 (1 byte Kana-SO/SI)
50225magicISO 2022 Korean
50227magicISO 2022 Simplified Chinese
51932Windows 7EUC Japanese
51936Windows 7EUC Simplified Chinese
51949Windows 7EUC Korean
52936Windows 7HZ-GB2312 Simplified Chinese
54936Windows 7GB18030 Simplified Chinese (4 byte)
57002Windows 7ISCII Devanagari
57003Windows 7ISCII Bengali
57004Windows 7ISCII Tamil
57005Windows 7ISCII Telugu
57006Windows 7ISCII Assamese
57007Windows 7ISCII Oriya
57008Windows 7ISCII Kannada
57009Windows 7ISCII Malayalam
57010Windows 7ISCII Gujarati
57011Windows 7ISCII Punjabi
65000magicUnicode (UTF-7)
65001magicUnicode (UTF-8)
unicode.org聽refers to the Unicode Consortium Public Mappings, a database of various mappings between Unicode characters and respective character sets. The tables are processed by a few scripts in the build process.
IBM聽refers to the IBM coded character set database. Even though IBM uses a different numbering scheme from Windows, the IBM numbers are used when there is no conflict. The tables are manually generated from the symbol manifests.
Windows 7聽refers to direct inspection of Windows 7 machines using .NET class聽System.Text.Encoding. The enclosed聽MakeEncoding.cs聽C# program brute-forces code pages.聽MakeEncoding.cs聽deviates from unicode.org in some cases. When they map a given code to different characters, unicode.org value is used. When unicode.org does not prescribe a value,聽MakeEncoding.cs聽value is used.
NLS聽refers to the National Language Support files supplied in various versions of Windows. In older versions of Windows (like Windows 98) these files followed the name pattern聽CP_#.NLS, but newer versions use the name pattern聽C_#.NLS.
Testing
make test聽will run the nodejs-based test.
To run the in-browser tests, run a local server and go to the聽ctest聽directory.聽make ctestserv聽will start a python聽SimpleHTTPServer聽server on port 8000.
To update the browser artifacts, run聽make ctest.
Sources
Unicode Consortium Public Mappings
Windows Code Page Enumeration
Windows Code Page Identifiers
IBM Coded Character Sets
ISO/IEC 2022 / ECMA-35
International Register of Coded Character Sets To Be Used With Escape Sequences
Japanese Character Encoding for Internet Messages