<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" />
<meta name="copyright" />
<title>计算字节长度</title>
<script>
var str = "abc中文";
// 4e00 - 9fa5
alert(getByteLength(str,"gbk"));
function getByteLength(str,type){
var len = 0;
for(var i = 0; i < str.length; i++){
if(str.charAt(i) >= "u4e00" && str.charAt(i) <= "u9fa5"){
if(type == "utf8"){
len += 3;
} else {
len += 2;
}
} else {
len++;
}
}
return len;
}
</script>
</head>
<body>
</body>
</html>
utf-8 中 存几个字节 byte = 8bit
gbk
utf-8 3字节
gbk 2字节
英文 1字节