Apq.Text.js

if( !Apq.Text.UTF8Encoding )
{
 ///

     // 1110 xxxx    10xx xxxx    10xx xxxx
     case 14:
      var n2 = Bytes[i++];
      var n3 = Bytes[i++];
      str += String.fromCharCode( ((n & 0x0F) << 12) | ((n2 & 0x3F) << 6) | (n3 & 0x3F) );
      break;
    }
   }
   return str;
  }
 };
 
 Apq.Text.Base64Encoding = {
  strEncode: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  /// 将字符转为对应的Base64序号('='对应-1)
  getEncode: function( c ){
   var n = c.charCodeAt(0);
   if( n >= 65 && n <= 90 )
   {
    return n - 65;
   }
   else if( n >= 97 && n <= 122 )
   {
    return n - 71; // n - 97 + 26
   }
   else if( n >= 48 && n <= 57 )
   {
    return n + 4; // n - 48 + 52
   }
   else if( c == '+' )
   {
    return 62;
   }
   else if( c == '/' )
   {
    return 63;
   }
   else
   {
    return -1;
   }
  },
  
  /// 解码
  /// <str>Base64字符串</str>
  GetBytes: function( str, index, length, Bytes, bindex ){
   index = index || 0;
   length = length || str.length;
   Bytes = Bytes || new Array();
   bindex = bindex || 0;
   
   var i = 0;
   while( i < length )
   {
    var cary = new Array();
    for( var j = 0; j < 4; j++ )
    {
     cary.push( this.getEncode( str.charAt(index+i++) ) );
    }
    
    Bytes[bindex++] = (cary[0] << 2) | (cary[1] >>> 4);
    var n = ((cary[1] << 4) | (cary[2] >>> 2)) & 0xFF;
    if( n != 0xF )
    {
     Bytes[bindex++] = n;
     n = ((cary[2] << 6) | (cary[3] & 0x3F )) & 0xFF;
     if( n != 0x3F )
     {
      Bytes[bindex++] = n;
     }
    }
   }
   return Bytes;
  },
  /// 编码
  /// <Bytes>UFT8数组</Bytes>
  GetString: function( Bytes, index, length ){
   index = index || 0;
   length = length || Bytes.length;
   
   var ary = new Array();
   
   var nRest = length % 3;
   var i = 0; // 位置指针
   while( i < length - nRest )
   {
    var bary = new Array();
    for( var j = 0; j < 3; j++ )
    {
     bary.push( Bytes[index+i+j] );
    }
    var n = i/3*4;
    ary[n] = this.strEncode.charAt( bary[0] >>> 2 );
    ary[n+1] = this.strEncode.charAt( ((bary[0] << 4) | (bary[1] >>> 4)) & 0x3F );
    ary[n+2] = this.strEncode.charAt( ((bary[1] << 2) | (bary[2] >>> 6)) & 0x3F );
    ary[n+3] = this.strEncode.charAt( bary[2] & 0x3F );
    i += 3;
   }
   
   if( nRest )
   {
    var bary = new Array();
    for( var j = 0; j < nRest; j++ )
    {
     bary.push( Bytes[index+i+j] );
    }
    bary.push( 0 );
    
    var n = i/3*4;
    ary[n] = this.strEncode.charAt( bary[0] >>> 2 );
    ary[n+1] = this.strEncode.charAt( ((bary[0] << 4) | (bary[1] >>> 4)) & 0x3F );
    if( nRest == 2 )
    {
     ary[n+2] = this.strEncode.charAt( (bary[1] << 2) & 0x3F );
    }
   
    for( var j = 0; j < 3 - nRest; j++ )
    {
     ary.push( "=" );
    }
   }
   
   return ary.join("");
  }
 };
}

转载于:https://www.cnblogs.com/Apq/archive/2006/06/14/425502.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值