/* UTF-8 <=> UTF-16 convertion library.
*
/* Copyright (C) 1999 Masanao Izumo <iz@onicos.co.jp>
* 2007 Ma Bingyao <andot@ujn.edu.cn>
* Version: 2.1
* LastModified: Feb 25, 2007
* This library is free. You can redistribute it and/or modify it.
*//*
* Interfaces:
* utf8 = utf16to8(utf16);
* utf16 = utf16to8(utf8);
*/function utf16to8(str) {
if (str.match(/^[\x00-\x7f]*$/) != null) {
return str;
}
var out, i, j, len, c, c2;
out = [];
len = str.length;
for (i = 0, j = 0; i < len; i++, j++) {
c = str.charCodeAt(i);
if (c <= 0x7f) {
out[j] = str.charAt(i);
}
else if (c <= 0x7ff) {
out[j] = String.fromCharCode(0xc0 | (c > 6),
0x80 | (c & 0x3f));
}
else if (c < 0xd800 || c > 0xdfff) {
out[j] = String.fromCharCode(0xe0 | (c > 12),
0x80 | ((c > 6) & 0x3f),
0x80 | (c & 0x3f));
}
else {
if (++i < len) {
c2 = str.charCodeAt(i);
if (c <= 0xdbff && 0xdc00 <= c2 && c2 <= 0xdfff) {
c = ((c & 0x03ff) << 10 | (c2 & 0x03ff)) + 0x010000;
if (0x010000 <= c && c <= 0x10ffff) {
out[j] = String.fromCharCode(0xf0 | ((c > 18) & 0x3f),
0x80 | ((c > 12) & 0x3f),
0x80 | ((c > 6) & 0x3f),
0x80 | (c & 0x3f));
}
else {
out[j] = '?';
}
}
else {
i--;
out[j] = '?';
}
}
else {
i--;
out[j] = '?';
}
}
}
return out.join('');
}function utf8to16(str) {
if ((str.match(/^[\x00-\x7f]*$/) != null) ||
(str.match(/^[\x00-\xff]*$/) == null)) {
return str;
}
var out, i, j, len, c, c2, c3, c4, s;out = [];
len = str.length;
i = j = 0;
while (i < len) {
c = str.charCodeAt(i++);
switch (c 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxx xxxx
out[j++] = str.charAt(i - 1);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
c2 = str.charCodeAt(i++);
out[j++] = String.fromCharCode(((c & 0x1f) << 6) |
(c2 & 0x3f));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
c2 = str.charCodeAt(i++);
c3 = str.charCodeAt(i++);
out[j++] = String.fromCharCode(((c & 0x0f) << 12) |
((c2 & 0x3f) << 6) |
(c3 & 0x3f));
break;
case 15:
switch (c & 0xf) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 1111 0xxx 10xx xxxx 10xx xxxx 10xx xxxx
c2 = str.charCodeAt(i++);
c3 = str.charCodeAt(i++);
c4 = str.charCodeAt(i++);
s = ((c & 0x07) << 18) |
((c2 & 0x3f) << 12) |
((c3 & 0x3f) << 6) |
(c4 & 0x3f) - 0x10000;
if (0 <= s && s <= 0xfffff) {
out[j] = String.fromCharCode(((s > 10) & 0x03ff) | 0xd800,
(s & 0x03ff) | 0xdc00);
}
else {
out[j] = '?';
}
break;
case 8: case 9: case 10: case 11:
// 1111 10xx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx
i+=4;
out[j] = '?';
break;
case 12: case 13:
// 1111 110x 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx 10xx xxxx
i+=5;
out[j] = '?';
break;
}
}
j++;
}
return out.join('');
}
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月25日
2024年11月25日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]