📄 ascii_7coding.java
字号:
import java.lang.Integer;
public class Ascii_7Coding implements Encodeing
{
private byte CodeToNum16(byte code){
switch( code ){
case (byte)'0':
return 0;
case (byte)'1':
return 1;
case (byte)'2':
return 2;
case (byte)'3':
return 3;
case (byte)'4':
return 4;
case (byte)'5':
return 5;
case (byte)'6':
return 6;
case (byte)'7':
return 7;
case (byte)'8':
return 8;
case (byte)'9':
return 9;
case (byte)'A':
case (byte)'a':
return 10;
case (byte)'B':
case (byte)'b':
return 11;
case (byte)'C':
case (byte)'c':
return 12;
case (byte)'D':
case (byte)'d':
return 13;
case (byte)'E':
case (byte)'e':
return 14;
case (byte)'F':
case (byte)'f':
return 15;
}
return 0;
};
//sometime , before encode , we need calc the number of
//hight ascii code in buffer;
public int CalcEncodeLengthForCharBuffer( byte[] sSrc ){
if( sSrc == null )
return 1;
int nLength = sSrc.length;
return CalcEncodeLength( sSrc , nLength );
};
public int CalcEncodeLength( byte[] sSrc , int nLength ){
if( sSrc==null )
return 1;
int nCount = 0;
for(int i=0;i<nLength;i++ ){
if( ( ( sSrc[i] & 0x80 ) != 0 )
||
( sSrc[i] == '=' )
||
( sSrc[i] < 0x20 )
)
nCount ++;
}
return nLength + nCount*2 + 1 + ( nLength / lineCode + 1 ) * 2 ;
};
//the buffer should be alloc and free by external module;
//Now ,it always return true;
public int Encode( byte[] sSrc,byte[] sDes,int nSrcLength,
int nOffsetSrc ,int nOffsetDes){
if( nSrcLength == 0 || sSrc==null || sDes==null ){
return 0;
}
int nDes=0;
String sTemp ;
for( int i=0 ; i<nSrcLength ; i++){
if( ( ( sSrc[nOffsetSrc+i] & 0x80 )!=0 )
|| ( sSrc[nOffsetSrc+i] == '=' )
|| ( sSrc[nOffsetSrc+i] < 0x20 )
){
sDes[nOffsetDes+nDes]=(byte)'=';
if( ( sSrc[nOffsetSrc+i] & 0xf0 ) != 0 ){
sDes[nOffsetDes+nDes+1]=(byte)( Integer.toHexString( (int)sSrc[nOffsetSrc+i] ) ).charAt(6);
sDes[nOffsetDes+nDes+2]=(byte)( Integer.toHexString( (int)sSrc[nOffsetSrc+i] ) ).charAt(7);
}
else{
sDes[nOffsetDes+nDes+1]=(byte)'0';
sDes[nOffsetDes+nDes+2]=(byte)( Integer.toHexString( (int)sSrc[nOffsetSrc+i] ) ).charAt(7);
}
nDes += 3;
}
else{
sDes[nOffsetDes+nDes]=sSrc[nOffsetSrc+i];
nDes ++ ;
}
}
return nDes;
};
//the buffer should be alloc and free by external module;
//Now ,it always return true;
public int Decode( byte[] sSrc,byte[] sDes,int nSrcLength
,int nOffsetSrc ,int nOffsetDes ){
int nDes = 0;
if( nSrcLength == 0 || sSrc==null || sDes==null ){
return 0;
}
for( int i=0;i<nSrcLength;i++ ){
//skip the '\r' '\n'
if( sSrc[nOffsetSrc+i] == 0xd || sSrc[nOffsetSrc+i] == 0xa ){
continue;
}
if( sSrc[nOffsetSrc+i] == 0 ){
return nDes;
}
if( sSrc[nOffsetSrc+i] == '=' ){
sDes[nOffsetDes+nDes] = (byte)( ( CodeToNum16(sSrc[nOffsetSrc+i+1]) )*16
+ CodeToNum16(sSrc[nOffsetSrc+i+2]) );
i+=2;
}
else{
sDes[nOffsetDes+nDes]=(byte)((sSrc[nOffsetSrc+i])&0x7f);
}
nDes++;
}
return nDes;
};
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -