网络编程
位置:首页>> 网络编程>> JavaScript>> js 返回 utf-8 字符编码是多少个字节, 函数 fUtf8BoundCheck

js 返回 utf-8 字符编码是多少个字节, 函数 fUtf8BoundCheck

  发布时间:2010-07-31 18:59:00 

标签:utf-8,编码,字符

 

<script type="text/javascript">
/*<![CDATA[*/
 var s = c = null;
 s = "&ccedil;";
 c = s.charCodeAt();
 document.write("<br/>s: "+s);
 document.write("<br/>c: "+c);
 document.write("<br/>fUtf8BoundCheck(c): "+fUtf8BoundCheck(c));
 document.write("<hr/>");
 s = "一";
 c = s.charCodeAt();
 document.write("<br/>s: "+s);
 document.write("<br/>c: "+c);
 document.write("<br/>fUtf8BoundCheck(c): "+fUtf8BoundCheck(c));
 document.write("<hr/>");
 s = "a";
 c = s.charCodeAt();
 document.write("<br/>s: "+s);
 document.write("<br/>c: "+c);
 document.write("<br/>fUtf8BoundCheck(c): "+fUtf8BoundCheck(c));
 document.write("<hr/>");
 
 function fUtf8BoundCheck(iCharCode)
 { // shawl.qiu code, return integer: Enum{1, 2, 3, 4}(meaning has byte x)
  if(iCharCode<0) throw new Error("arg must be integer!");
  
  var LBound = null, UBound = null;
  
  LBound = 0; UBound = parseInt("7F", 16);
  if(iCharCode>=LBound&&iCharCode<=UBound) return 1;
  
  LBound = parseInt(80, 16); UBound = parseInt("7FF", 16);
  if(iCharCode>=LBound&&iCharCode<=UBound) return 2;
  
  LBound = parseInt(800, 16); UBound = parseInt("FFFF", 16);
  if(iCharCode>=LBound&&iCharCode<=UBound) return 3;
  
  LBound = parseInt(10000 , 16); UBound = parseInt("10FFFF", 16);
  if(iCharCode>=LBound&&iCharCode<=UBound) return 4;
  
  throw new Error("out of bound!");
 } // end function fUtf8BoundCheck(iCharCode);
/*]]*/
</script>

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com