hex.h

来自「各种加密算法的源代码」· C头文件 代码 · 共 65 行

H
65
字号
#ifndef HEX_H
#define HEX_H

#include "cryptlib.h"
#include "filters.h"

class HexEncoder : public Filter
{
public:
    HexEncoder(BufferedTransformation *outQueue = new ByteQueue)
        : Filter(outQueue) {}

    void Put(byte inByte)
    {
        outQueue->Put(vec[inByte >> 4]);
        outQueue->Put(vec[inByte & 0x0F]);
    }

    void Put(const byte *inString, unsigned int length);

private:
    static const byte vec[17];
};

class HexDecoder : public Filter
{
public:
    HexDecoder(BufferedTransformation *outQueue = new ByteQueue)
        : Filter(outQueue) {last = -1;}

    void Put(byte inByte)
    {
        int i=ConvToNumber(inByte);
        if (i >= 0)
        {
            if (last >= 0)
            {
                outQueue->Put((last << 4) | i);
                last = -1;
            }
            else
                last = i;
        }
    }

    void Put(const byte *inString, unsigned int length);

private:
    static int ConvToNumber(byte inByte)
    {
        if (inByte >= '0' && inByte <= '9')
            return inByte - '0';
        if (inByte >= 'A' && inByte <= 'F')
            return inByte - 'A' + 10;
        if (inByte >= 'a' && inByte <= 'f')
            return inByte - 'a' + 10;
        return -1;
    }

    int last;
};

#endif

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?