答复
给出一个代码;)
代码:
void bEncode(UINT symbol)
{
UINT u = (this->uiSom >> 11) * this->Prob; // 变小,大概1/2左右,看Prob是大于0x400还是小于0x400
if(symbol == 0){// uiAny始终比uiSom小
this->uiSom = u;
this->Prob += ((1 << 11) - this->Prob) >> 5;
}
else{ // uiAny >= u
//uiAny -= u;
this->Low += u;
this->uiSom -= u;
this->Prob -= (this->Prob) >> 5;// prob 变小,减少1/32,如果一直输入一,则prob会变得很小-0x1f
}
if(this->uiSom < (1 << 24)){
//如果最高位8位全0,写入一个字节,同时左移8位
this->ShiftLow();
this->uiSom <<= 8;
}
}
void ShiftLow()
{
if(_cacheSize)
{
if(_cache == 0xff && (Low>>32 & 0x000000ff) )
{
int i;
for (i=1; m_pbuf[m_len-i]==0xff&&(m_len-i)>=0;i++)
m_pbuf[m_len-i]=0;
if((m_len-i)>=0)m_pbuf[m_len-i]++;
else
{
for(i=m_len++; i>=0; i--)
m_pbuf[i]=m_pbuf[i-1];
m_pbuf[0]=1;
}
}
WriteByte(((Low>>32 & 0x000000ff) + _cache));
_cacheSize--;
}
_cache = ((Low >>24) & 0x000000ff);
_cacheSize++;
Low <<= 8;
Low &= 0xffffffff;
}
void FlushData()
{
for(int i = 0; i<_cacheSize+sizeof(DWORD)/sizeof(BYTE);i++)
ShiftLow();
}
