JAVASCRIPT/자바 스크립트 Byte 체크

자바스크립트 한글 byte 계산 함수

정윤재 2011. 1. 11. 13:52

 //바이트계산 함수
 function calculate_msglen(msg)
 {
  var nbytes = 0;
  for (i=0; i<msg.length; i++) {
   var ch = msg.charAt(i);
   if(escape(ch).length > 4) {
    nbytes += 2;
   } else if (ch == '\n') {
    if (msg.charAt(i-1) != '\r') {
     nbytes += 1;
    }
   } else if (ch == '<' || ch == '>') {
    nbytes += 4;
   } else {
    nbytes += 1;
   }
  }
  return nbytes;
 }