lib.utf.js


Posted in Javascript onAugust 21, 2007
/* 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('');
}
Javascript 相关文章推荐
如何用javascript控制上传文件的大小
Oct 26 Javascript
List the UTC Time on a Computer
Jun 11 Javascript
JS函数实现动态添加CSS样式表文件
Dec 15 Javascript
js创建表单元素并使用submit进行提交
Aug 14 Javascript
javascript中定义类的方法详解
Feb 10 Javascript
JS比较两个数值的大小实例
Nov 25 Javascript
如何解决jQuery EasyUI 已打开Tab重新加载问题
Dec 19 Javascript
jquery pagination分页插件使用详解(后台struts2)
Jan 22 Javascript
原生JS实现图片网格式渐显、渐隐效果
Jun 05 Javascript
20行JS代码实现网页刮刮乐效果
Jun 23 Javascript
js与jQuery实现获取table中的数据并拼成json字符串操作示例
Jul 12 jQuery
Vue实战教程之仿肯德基宅急送App
Jul 19 Javascript
javascript实现上传图片前的预览(TX的面试题)
Aug 20 #Javascript
fix-ie5.js扩展在IE5下不能使用的几个方法
Aug 20 #Javascript
关于恒等于(===)和非恒等于(!==)
Aug 20 #Javascript
图片格式的JavaScript和CSS速查手册
Aug 20 #Javascript
javascript 一个自定义长度的文本自动换行的函数
Aug 19 #Javascript
js类中获取外部函数名的方法
Aug 19 #Javascript
写了一个layout,拖动条连贯,内容区可为iframe
Aug 19 #Javascript
You might like
PHP中文URL编解码(urlencode()rawurlencode()
2010/07/03 PHP
PHP防CC攻击实现代码
2011/12/29 PHP
php编写的简单页面跳转功能实现代码
2013/11/27 PHP
php对数组排序的简单实例
2013/12/25 PHP
PHP连接及操作PostgreSQL数据库的方法详解
2019/01/30 PHP
laravel异步监控定时调度器实例详解
2019/06/21 PHP
ThinkPHP5&amp;5.1框架关联模型分页操作示例
2019/08/03 PHP
自适应高度框架 ----属个人收藏内容
2007/01/22 Javascript
asp.net+jquery滚动滚动条加载数据的下拉控件
2010/06/25 Javascript
jquery eval解析JSON中的注意点介绍
2013/08/23 Javascript
原生JS实现仿淘宝网左侧商品分类菜单效果代码
2015/09/10 Javascript
jQuery获取cookie值及删除cookie用法实例
2016/04/15 Javascript
JavaScript中的toString()和toLocaleString()方法的区别
2017/02/15 Javascript
实例分析nodejs模块xml2js解析xml过程中遇到的坑
2017/03/18 NodeJs
基于vue的fullpage.js单页滚动插件
2017/03/20 Javascript
vue项目中vue-i18n和element-ui国际化开发实现过程
2018/04/25 Javascript
vue里面使用mui的弹出日期选择插件实例
2018/09/16 Javascript
JavaScript的词法结构精华篇
2018/10/17 Javascript
微信小程序canvas绘制圆角base64图片的实现
2019/08/18 Javascript
小程序按钮避免多次调用接口和点击方案实现(不用showLoading)
2020/04/15 Javascript
Python 时间处理datetime实例
2008/09/06 Python
python client使用http post 到server端的代码
2013/02/10 Python
python网络编程学习笔记(二):socket建立网络客户端
2014/06/09 Python
Python正则表达式的使用范例详解
2014/08/08 Python
Python中Collections模块的Counter容器类使用教程
2016/05/31 Python
Python实现的大数据分析操作系统日志功能示例
2019/02/11 Python
Python实现去除列表中重复元素的方法总结【7种方法】
2019/02/16 Python
python语言元素知识点详解
2019/05/15 Python
对Python3之方法的覆盖与super函数详解
2019/06/26 Python
详解利用OpenCV提取图像中的矩形区域(PPT屏幕等)
2019/07/01 Python
实用的简历自我评价
2014/03/06 职场文书
四风批评与自我批评范文
2014/10/14 职场文书
综治维稳工作汇报
2014/10/27 职场文书
房产分割协议书范文
2014/11/21 职场文书
2015年党务工作者个人工作总结
2015/10/22 职场文书
教你nginx跳转配置的四种方式
2022/07/07 Servers