常用编码详解
2007-07-21 21:36:06 来源:WEB开发网核心提示: 1)正处于Base64编码状态中2)正处于直接编码状态中3)现在UTF-7的缓冲区里,当前的字符是转换开关"+"所以要定义相关的字段:// the state of current character#defineIN_ASCII0#defineIN_BASE641#d
1)正处于Base64编码状态中
2)正处于直接编码状态中
3)现在UTF-7的缓冲区里,当前的字符是转换开关"+"
所以要定义相关的字段:
// the state of current character
#define IN_ASCII 0
#define IN_BASE64 1
#define AFTER_PLUS 2
在使用规则2进行编码时候,需要使用base64的方法,也就需要2个全局的辅助变量:int state; // state in which we are working
int nbits; // number of bits in the bit buffer
unsigned long bitBuffer; // used for base64 coding
把一个Unicode字符转化为一个UTF-7序列:返回写到缓冲区里的字节数目,函数影响了state,nbits,bitBuffer三个全局变量。这里先实现了一个简单的辅助函数,功能是把一个Unicode字符转变后写到提供的缓冲区中,返回写入的字节个数。在开始编码Unicode字符数组中第一个字符的时候,state,nbits,bitBuffer三个全局变量需要被初始化:state = IN_ASCII;
nbits = 0;
bitBuffer = 0;
int UnicodeToUTF7(WCHAR ucs2, byte *buffer)
{
byte *head = buffer;
int index;
// is an ASCII and is a byte in char set defined
if (((ucs2 & 0xff80) == 0)) && (byteType[(byte)u2] & (BASE64|SAFE|SPACE)))
{
byte temp = (byte)ucs2;
if (state == IN_BASE64) // should switch out from base64 coding here
{
if (nbits > 0) // if some bits in buffer, then output them
{
index = (bitBuffer << (6 - nbits)) & 0x3f;
*s++ = base64[index];
}
if ((byteType[temp] & BASE64) || (temp == ''-''))
*s++ = ''-'';
state = IN_ASCII;
}
*s++ = temp;
if (temp == ''+'')
*s++ = ''-'';
}
else
{
if (state == IN_ASCII)
{
*s++ = ''+'';
state = IN_BASE64; // begins base64 coding here
nbits = 0;
bitBuffer = 0;
}
bitBuffer <<= 16;
bitBuffer |= ucs2;
nbits += 16;
while(nbits >= 6)
{
nbits -= 6;
index = (bitBuffer >> nbits) & 0x3f; // output the high 6 bits
*s++ = base64[index];
}
}
return (s - head);
}
[]
更多精彩
赞助商链接