node.js读取来自stdin的ä,ö等字母

时间:2014-09-22 14:59:46

标签: javascript node.js stdin

我尝试使用以下代码

读取node.js中的stdin
process.stdin.setEncoding("utf8");
process.stdout.setEncoding("utf8");
process.stdin.on('data', function(data) {
    process.stdout.write('data: ' + data.toString('utf8'));
});

当我输入类似

的字符串时
ÜäÖTest 

写道

data: ???Test

如何从控制台获取正确的字母?

编辑:我发现,缓冲区中的字符代码显示了来自 ascii table 的字母,例如字母“ü”的129。但普通String中的相同字母具有来自 ansi表的charcode 252。现在我必须找到一种方法来改变这些特殊字母的字符。

1 个答案:

答案 0 :(得分:0)

问题是,Windows控制台使用代码页850,javascript字符串通常由unicode构成。所以我从一个cp850映射到unicode

var cp850toUnicode = {0x10:"\u0010",0x11:"\u0011",0x12:"\u0012",0x13:"\u0013",0x14:"\u0014",0x15:"\u0015",0x16:"\u0016",0x17:"\u0017",0x18:"\u0018",0x19:"\u0019",0x20:"\u0020",0x21:"\u0021",0x22:"\u0022",0x23:"\u0023",0x24:"\u0024",0x25:"\u0025",0x26:"\u0026",0x27:"\u0027",0x28:"\u0028",0x29:"\u0029",0x30:"\u0030",0x31:"\u0031",0x32:"\u0032",0x33:"\u0033",0x34:"\u0034",0x35:"\u0035",0x36:"\u0036",0x37:"\u0037",0x38:"\u0038",0x39:"\u0039",0x40:"\u0040",0x41:"\u0041",0x42:"\u0042",0x43:"\u0043",0x44:"\u0044",0x45:"\u0045",0x46:"\u0046",0x47:"\u0047",0x48:"\u0048",0x49:"\u0049",0x50:"\u0050",0x51:"\u0051",0x52:"\u0052",0x53:"\u0053",0x54:"\u0054",0x55:"\u0055",0x56:"\u0056",0x57:"\u0057",0x58:"\u0058",0x59:"\u0059",0x60:"\u0060",0x61:"\u0061",0x62:"\u0062",0x63:"\u0063",0x64:"\u0064",0x65:"\u0065",0x66:"\u0066",0x67:"\u0067",0x68:"\u0068",0x69:"\u0069",0x70:"\u0070",0x71:"\u0071",0x72:"\u0072",0x73:"\u0073",0x74:"\u0074",0x75:"\u0075",0x76:"\u0076",0x77:"\u0077",0x78:"\u0078",0x79:"\u0079",0x80:"\u00c7",0x81:"\u00fc",0x82:"\u00e9",0x83:"\u00e2",0x84:"\u00e4",0x85:"\u00e0",0x86:"\u00e5",0x87:"\u00e7",0x88:"\u00ea",0x89:"\u00eb",0x90:"\u00c9",0x91:"\u00e6",0x92:"\u00c6",0x93:"\u00f4",0x94:"\u00f6",0x95:"\u00f2",0x96:"\u00fb",0x97:"\u00f9",0x98:"\u00ff",0x99:"\u00d6",0x00:"\u0000",0x01:"\u0001",0x02:"\u0002",0x03:"\u0003",0x04:"\u0004",0x05:"\u0005",0x06:"\u0006",0x07:"\u0007",0x08:"\u0008",0x09:"\u0009",0x0a:"\u000a",0x0b:"\u000b",0x0c:"\u000c",0x0d:"\u000d",0x0e:"\u000e",0x0f:"\u000f",0x1a:"\u001a",0x1b:"\u001b",0x1c:"\u001c",0x1d:"\u001d",0x1e:"\u001e",0x1f:"\u001f",0x2a:"\u002a",0x2b:"\u002b",0x2c:"\u002c",0x2d:"\u002d",0x2e:"\u002e",0x2f:"\u002f",0x3a:"\u003a",0x3b:"\u003b",0x3c:"\u003c",0x3d:"\u003d",0x3e:"\u003e",0x3f:"\u003f",0x4a:"\u004a",0x4b:"\u004b",0x4c:"\u004c",0x4d:"\u004d",0x4e:"\u004e",0x4f:"\u004f",0x5a:"\u005a",0x5b:"\u005b",0x5c:"\u005c",0x5d:"\u005d",0x5e:"\u005e",0x5f:"\u005f",0x6a:"\u006a",0x6b:"\u006b",0x6c:"\u006c",0x6d:"\u006d",0x6e:"\u006e",0x6f:"\u006f",0x7a:"\u007a",0x7b:"\u007b",0x7c:"\u007c",0x7d:"\u007d",0x7e:"\u007e",0x7f:"\u007f",0x8a:"\u00e8",0x8b:"\u00ef",0x8c:"\u00ee",0x8d:"\u00ec",0x8e:"\u00c4",0x8f:"\u00c5",0x9a:"\u00dc",0x9b:"\u00f8",0x9c:"\u00a3",0x9d:"\u00d8",0x9e:"\u00d7",0x9f:"\u0192",0xa0:"\u00e1",0xa1:"\u00ed",0xa2:"\u00f3",0xa3:"\u00fa",0xa4:"\u00f1",0xa5:"\u00d1",0xa6:"\u00aa",0xa7:"\u00ba",0xa8:"\u00bf",0xa9:"\u00ae",0xaa:"\u00ac",0xab:"\u00bd",0xac:"\u00bc",0xad:"\u00a1",0xae:"\u00ab",0xaf:"\u00bb",0xb0:"\u2591",0xb1:"\u2592",0xb2:"\u2593",0xb3:"\u2502",0xb4:"\u2524",0xb5:"\u00c1",0xb6:"\u00c2",0xb7:"\u00c0",0xb8:"\u00a9",0xb9:"\u2563",0xba:"\u2551",0xbb:"\u2557",0xbc:"\u255d",0xbd:"\u00a2",0xbe:"\u00a5",0xbf:"\u2510",0xc0:"\u2514",0xc1:"\u2534",0xc2:"\u252c",0xc3:"\u251c",0xc4:"\u2500",0xc5:"\u253c",0xc6:"\u00e3",0xc7:"\u00c3",0xc8:"\u255a",0xc9:"\u2554",0xca:"\u2569",0xcb:"\u2566",0xcc:"\u2560",0xcd:"\u2550",0xce:"\u256c",0xcf:"\u00a4",0xd0:"\u00f0",0xd1:"\u00d0",0xd2:"\u00ca",0xd3:"\u00cb",0xd4:"\u00c8",0xd5:"\u0131",0xd6:"\u00cd",0xd7:"\u00ce",0xd8:"\u00cf",0xd9:"\u2518",0xda:"\u250c",0xdb:"\u2588",0xdc:"\u2584",0xdd:"\u00a6",0xde:"\u00cc",0xdf:"\u2580",0xe0:"\u00d3",0xe1:"\u00df",0xe2:"\u00d4",0xe3:"\u00d2",0xe4:"\u00f5",0xe5:"\u00d5",0xe6:"\u00b5",0xe7:"\u00fe",0xe8:"\u00de",0xe9:"\u00da",0xea:"\u00db",0xeb:"\u00d9",0xec:"\u00fd",0xed:"\u00dd",0xee:"\u00af",0xef:"\u00b4",0xf0:"\u00ad",0xf1:"\u00b1",0xf2:"\u2017",0xf3:"\u00be",0xf4:"\u00b6",0xf5:"\u00a7",0xf6:"\u00f7",0xf7:"\u00b8",0xf8:"\u00b0",0xf9:"\u00a8",0xfa:"\u00b7",0xfb:"\u00b9",0xfc:"\u00b3",0xfd:"\u00b2",0xfe:"\u25a0",0xff:"\u00a0"}

process.stdin.on('data', function(data) {
    var characters = [];
    for(var i = 0; i < data.length-2;i++)
        characters.push(cp850toUnicode[data[i]]);
    characters = characters.join('');
    console.log(characters)
});