Last Updated 2013/01/18
Programming Tips Web  索 引 
文字列バイト数
2013/01/18

JavaScript で ShiftJIS での文字列のバイト数は以下でもとめる。
(半角は1バイト,全角は2バイト計算)

    var iLength;
    var iCnt;
    
    iLength = 0;
    if( strValue.length == 0 ) return iLength;
    
    for( iCnt = 0; iCnt < strValue.length; iCnt++ ) {
        strTemp = escape( strValue.charAt(iCnt) );
        if( strTemp.length < 4 )
            iLength++;
        else
            iLength += 2;
    }
    return iLength;


参照
前後のTips
文字列バイト数

DSS ProgrammingTipsCGI Ver2.02