• 5274阅读
  • 2回复

请指教这段代码的含义 [复制链接]

上一主题 下一主题
离线channon
 
只看楼主 正序阅读 楼主  发表于: 2009-11-11
因为要用java生成压缩数据,然后用quncompress解压,所以看了qcompress的代码。

QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel)
{
    if (nbytes == 0) {
        return QByteArray(4, '\0');
    }
    if (!data) {
        qWarning("qCompress: Data is null");
        return QByteArray();
    }
    if (compressionLevel < -1 || compressionLevel > 9)
        compressionLevel = -1;

    ulong len = nbytes + nbytes / 100 + 13;
    QByteArray bazip;
    int res;
    do {
        bazip.resize(len + 4);
        res = ::compress2((uchar*)bazip.data()+4, &len, (uchar*)data, nbytes, compressionLevel);

        switch (res) {
        case Z_OK:
            bazip.resize(len + 4);
            bazip[0] = (nbytes & 0xff000000) >> 24;
            bazip[1] = (nbytes & 0x00ff0000) >> 16;
            bazip[2] = (nbytes & 0x0000ff00) >> 8;
            bazip[3] = (nbytes & 0x000000ff);
            break;
        case Z_MEM_ERROR:
            qWarning("qCompress: Z_MEM_ERROR: Not enough memory");
            bazip.resize(0);
            break;
        case Z_BUF_ERROR:
            len *= 2;
            break;
        }
    } while (res == Z_BUF_ERROR);

    return bazip;
}

对其中
            bazip[0] = (nbytes & 0xff000000) >> 24;
            bazip[1] = (nbytes & 0x00ff0000) >> 16;
            bazip[2] = (nbytes & 0x0000ff00) >> 8;
            bazip[3] = (nbytes & 0x000000ff);

的含义不太明白,请指教。
操作系统: Archlinux
Qt SDK: Qt 4.7.1 for Linux
开发方向: Qt,Java
常用数据库:Firebird、SQLite
离线channon
只看该作者 2楼 发表于: 2009-11-12
thanks,了解点了。
操作系统: Archlinux
Qt SDK: Qt 4.7.1 for Linux
开发方向: Qt,Java
常用数据库:Firebird、SQLite
离线steinlee

只看该作者 1楼 发表于: 2009-11-12
You can use a simple example to understand it.
int a = 5; //=00000101
int b = 6; //=00000110
int c = a & b;//=00000100<==4
00000101(5)
00000110(6)
=========&(做和)
00000100(4)
int d = c >> 1 ;// 00000010 <== 向右移一位
结果d=2
==============================
bazip[0] = (nbytes & 0xff000000) >> 24;
bazip[1] = (nbytes & 0x00ff0000) >> 16;
bazip[2] = (nbytes & 0x0000ff00) >> 8;
bazip[3] = (nbytes & 0x000000ff);
Looking for remote C/C++ and Qt 兼职
快速回复
限100 字节
 
上一个 下一个