728x90

[소스코드]

          /**
           * @description 문자열 길이(Byte)를 구하는 함수.
           * @param {string} 문자
           * @param {string} charset (UTF-8,EUC-KR)
           * @return {string} 문자열 길이(Byte)
           */
          lengthB: function (s, charset, b, i, c) {
                
                // argv 값 Validation 은 추가 필요합니다.
                if( s == '' ){
                    b = -1;    
                }else{
                    charset = charset ? charset.toUpperCase() : 'UTF-8';
                    
                    if( charset.search(/UTF-8|EUC-KR/g) > -1 ){
                        if(charset == 'UTF-8'){
                            for(b=i=0;c=s.charCodeAt(i++);b+=c>>11?3:c>>7?2:1);
                        }else{
                            for(b=i=0;c=s.charCodeAt(i++);b+=c>>11?2:c>>7?2:1);
                        }
                    }
                }
                
                console.log('입력값 : |' + s + '| 캐릭터셋 : |' + charset + '| >> 출력값 : |' + b + '(byte)|'); 

                return b;
          },

 

[실행결과]

[첨부파일]

test.html
0.00MB

728x90
반응형

+ Recent posts