Posted on 2006-11-08 13:32
小明 阅读(2487)
评论(0) 编辑 收藏 引用 所属分类:
Tools 、
G11N/ICU
项目需要,写了一个帮助L10N的工程师计算utf8的小工具(html page)。如下
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> An utf8 count tool </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=utf16">
<script>
function utf16to8(str) {
var out, i, len, c;
out = "";
len = str.length;
for(i = 0; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
function count()
{
var temp = f1.value;
temp=temp.replace(/\\r/g,"\r");
temp=temp.replace(/\\n/g,"\n");
result.innerHTML=utf16to8(temp).length;
}
</script>
</HEAD>
<BODY>
<TEXTAREA id="f1" NAME="f1" ROWS="10" COLS="50"></TEXTAREA>
<br/>
<INPUT TYPE="button" value="count length" onclick="count()">
<div id="result"></div>
</BODY>
</HTML>