Кодировка UTF-8
Здравствуйте.
У меня к вам вопрос.
Создана страница test.html в кодировке UTF-8.
Русский текст и функции работают исправно.
Подключил 1 модуль cgi, который работает только через win-1251.
Как не меняя кодировки test.html, отправлять запросы к модулю
cgi, работающий через win-1251 и получать русские буквы, а не черные ромбы с вопросительными знаками?
Пробывал следующие функции, но пока что-то без результата:
function utf8_encode(string) {
string = string.replace(/rn/g,"n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
}
function utf8_decode(utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
function win2unicode(str) {
var charmap = unescape(
"%u0402%u0403%u201A%u0453%u201E%u2026%u2020%u2021% u20AC%u2030%u0409%u2039%u040A%u040C%u040B%u040F"+
"%u0452%u2018%u2019%u201C%u201D%u2022%u2013%u2014% u0000%u2122%u0459%u203A%u045A%u045C%u045B%u045F"+
"%u00A0%u040E%u045E%u0408%u00A4%u0490%u00A6%u00A7% u0401%u00A9%u0404%u00AB%u00AC%u00AD%u00AE%u0407"+
"%u00B0%u00B1%u0406%u0456%u0491%u00B5%u00B6%u00B7% u0451%u2116%u0454%u00BB%u0458%u0405%u0455%u0457")
var code2char = function(code) {
if(code >= 0xC0 && code <= 0xFF) return String.fromCharCode(code - 0xC0 + 0x0410)
if(code >= 0x80 && code <= 0xBF) return charmap.charAt(code - 0x80)
return String.fromCharCode(code) ;
}
var res = ""
for(var i = 0; i < str.length; i++) res = res + code2char(str.charCodeAt(i))
return res;
}
var DMap = {0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99, 100: 100, 101: 101, 102: 102, 103: 103, 104: 104, 105: 105, 106: 106, 107: 107, 108: 108, 109: 109, 110: 110, 111: 111, 112: 112, 113: 113, 114: 114, 115: 115, 116: 116, 117: 117, 118: 118, 119: 119, 120: 120, 121: 121, 122: 122, 123: 123, 124: 124, 125: 125, 126: 126, 127: 127, 1027: 129, 8225: 135, 1046: 198, 8222: 132, 1047: 199, 1168: 165, 1048: 200, 1113: 154, 1049: 201, 1045: 197, 1050: 202, 1028: 170, 160: 160, 1040: 192, 1051: 203, 164: 164, 166: 166, 167: 167, 169: 169, 171: 171, 172: 172, 173: 173, 174: 174, 1053: 205, 176: 176, 177: 177, 1114: 156, 181: 181, 182: 182, 183: 183, 8221: 148, 187: 187, 1029: 189, 1056: 208, 1057: 209, 1058: 210, 8364: 136, 1112: 188, 1115: 158, 1059: 211, 1060: 212, 1030: 178, 1061: 213, 1062: 214, 1063: 215, 1116: 157, 1064: 216, 1065: 217, 1031: 175, 1066: 218, 1067: 219, 1068: 220, 1069: 221, 1070: 222, 1032: 163, 8226: 149, 1071: 223, 1072: 224, 8482: 153, 1073: 225, 8240: 137, 1118: 162, 1074: 226, 1110: 179, 8230: 133, 1075: 227, 1033: 138, 1076: 228, 1077: 229, 8211: 150, 1078: 230, 1119: 159, 1079: 231, 1042: 194, 1080: 232, 1034: 140, 1025: 168, 1081: 233, 1082: 234, 8212: 151, 1083: 235, 1169: 180, 1084: 236, 1052: 204, 1085: 237, 1035: 142, 1086: 238, 1087: 239, 1088: 240, 1089: 241, 1090: 242, 1036: 141, 1041: 193, 1091: 243, 1092: 244, 8224: 134, 1093: 245, 8470: 185, 1094: 246, 1054: 206, 1095: 247, 1096: 248, 8249: 139, 1097: 249, 1098: 250, 1044: 196, 1099: 251, 1111: 191, 1055: 207, 1100: 252, 1038: 161, 8220: 147, 1101: 253, 8250: 155, 1102: 254, 8216: 145, 1103: 255, 1043: 195, 1105: 184, 1039: 143, 1026: 128, 1106: 144, 8218: 130, 1107: 131, 8217: 146, 1108: 186, 1109: 190}
function UnicodeToWin1251(s) {
var L = []
for (var i=0; i<s.length; i++) {
var ord = s.charCodeAt(i)
if (!(ord in DMap))
throw "Character "+s.charAt(i)+" isn't supported by win1251!"
L.push(String.fromCharCode(DMap[ord]))
}
return L.join('')
}
// Encodes UCS2 into UTF8
// Returns an array of numbers (bytes)
function encode(str) {
var len = str.length;
var result = [];
var code;
var i;
for (i = 0; i < len; i++) {
code = str.charCodeAt(i);
if (code <= 0x7f) {
result.push(code);
} else if (code <= 0x7ff) { // 2 bytes
result.push(0xc0 | (code >>> 6 & 0x1f),
0x80 | (code & 0x3f));
} else if (code <= 0xd700 || code >= 0xe000) { // 3 bytes
result.push(0xe0 | (code >>> 12 & 0x0f),
0x80 | (code >>> 6 & 0x3f),
0x80 | (code & 0x3f));
} else { // 4 bytes, surrogate pair
code = (((code - 0xd800) << 10) | (str.charCodeAt(++i) - 0xdc00)) + 0x10000;
result.push(0xf0 | (code >>> 18 & 0x07),
0x80 | (code >>> 12 & 0x3f),
0x80 | (code >>> 6 & 0x3f),
0x80 | (code & 0x3f));
}
}
return result;
}
// Decodes UTF8 into UCS2
// Returns a string
function decode(bytes) {
var len = bytes.length;
var result = "";
var code;
var i;
for (i = 0; i < len; i++) {
if (bytes[i] <= 0x7f) {
result += String.fromCharCode(bytes[i]);
} else if (bytes[i] >= 0xc0) { // Mutlibytes
if (bytes[i] < 0xe0) { // 2 bytes
code = ((bytes[i++] & 0x1f) << 6) |
(bytes[i] & 0x3f);
} else if (bytes[i] < 0xf0) { // 3 bytes
code = ((bytes[i++] & 0x0f) << 12) |
((bytes[i++] & 0x3f) << 6) |
(bytes[i] & 0x3f);
} else { // 4 bytes
// turned into two characters in JS as surrogate pair
code = (((bytes[i++] & 0x07) << 18) |
((bytes[i++] & 0x3f) << 12) |
((bytes[i++] & 0x3f) << 6) |
(bytes[i] & 0x3f)) - 0x10000;
// High surrogate
result += String.fromCharCode(((code & 0xffc00) >>> 10) + 0xd800);
// Low surrogate
code = (code & 0x3ff) + 0xdc00;
}
result += String.fromCharCode(code);
} // Otherwise it's an invalid UTF-8, skipped.
}
return result;
}
function encode_utf81( s )
{
return unescape( encodeURIComponent( s ) );
}
function decode_utf81( s )
{
return decodeURIComponent( escape( s ) );
}
|