计算utf8下的字节数的方法
计算utf8下的字节数的方法
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><HTML>
<HEAD>
<TITLE>Anutf8counttool</TITLE>
<metahttp-equiv="Content-Type"content="text/html;charset=utf16">
<script>
functionutf16to8(str){
varout,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);
}elseif(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));
}
}
returnout;
}
functioncount()
{
vartemp=f1.value;
temp=temp.replace(///r/g,"/r");
temp=temp.replace(///n/g,"/n");
result.innerHTML=utf16to8(temp).length;
}
</script>
</HEAD>
<BODY>
<TEXTAREAid="f1"NAME="f1"ROWS="10"COLS="50"></TEXTAREA>
<br/>
<INPUTTYPE="button"value="countlength"onclick="count()">
<divid="result"></div>
</BODY>
</HTML>