1function dec2char(n) { 2 // converts a decimal number to a Unicode character 3 // n: the dec codepoint value to be converted 4 if (n <= 0xffff) { 5 out = String.fromCharCode(n); 6 } else if (n <= 0x10ffff) { 7 n -= 0x10000; 8 out = 9 String.fromCharCode(0xd800 | (n >> 10)) + 10 String.fromCharCode(0xdc00 | (n & 0x3ff)); 11 } else out = "dec2char error: Code point out of range: " + n; 12 return out; 13} 14 15function euckrDecoder(stream) { 16 stream = stream.replace(/%/g, " "); 17 stream = stream.replace(/[\s]+/g, " ").trim(); 18 var bytes = stream.split(" "); 19 for (var i = 0; i < bytes.length; i++) bytes[i] = parseInt(bytes[i], 16); 20 var out = ""; 21 var lead, byte, offset, ptr, cp; 22 var euckrLead = 0x00; 23 var endofstream = 2000000; 24 var finished = false; 25 26 while (!finished) { 27 if (bytes.length == 0) byte = endofstream; 28 else byte = bytes.shift(); 29 30 if (byte == endofstream && euckrLead != 0x00) { 31 euckrLead = 0x00; 32 out += "�"; 33 continue; 34 } 35 if (byte == endofstream && euckrLead == 0x00) { 36 finished = true; 37 continue; 38 } 39 40 if (euckrLead != 0x00) { 41 lead = euckrLead; 42 ptr = null; 43 euckrLead = 0x00; 44 if (byte >= 0x41 && byte <= 0xfe) 45 ptr = (lead - 0x81) * 190 + (byte - 0x41); 46 if (ptr == null) cp = null; 47 else cp = euckr[ptr]; 48 if (cp == null && byte >= 0x00 && byte <= 0x7f) bytes.unshift(byte); 49 if (cp == null) { 50 out += "�"; 51 continue; 52 } 53 out += dec2char(cp); 54 continue; 55 } 56 if (byte >= 0x00 && byte <= 0x7f) { 57 out += dec2char(byte); 58 continue; 59 } 60 if (byte >= 0x81 && byte <= 0xfe) { 61 euckrLead = byte; 62 continue; 63 } 64 out += "�"; 65 } 66 return out; 67} 68