C ++:将ushort,uint和ulong转换为vector <uchar>

时间:2017-07-20 23:58:23

标签: c++ type-conversion

我正在构建一个数字类来处理1到8个字节。我的想法是:按stringchar*输入值,然后程序根据字符串将其转换为unsigned shortunsigned intunsigned long size(我使用stringstream进行转换,这部分没问题)。程序将字节值分隔后,将它们存储在vector<unsigned char>中。要输出数字,程序会根据字节数将vector<unsigned char>转换为unsigned shortunsigned intunsigned long,然后转换为string。<登记/> 问题是:程序存储/显示的值与输入的值不同 这是完整的代码:

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;

class Num
{
private:
    vector<uchar> x;
    ushort len;

    template<typename T>
    string str()
    {
        T val = 0;
        for(ushort i = 0; i < len; i++)
        {
            val += x[i] * pow(256, i);
        }

        stringstream res;
        res << val;

        return res.str();
    }

    template<typename T>
    void set(string a)
    {
        T val;
        stringstream(a) >> val;

        x.resize(0);
        len = 0;

        while(val > 255)
        {
            x.push_back((T)(val % 256));
            len++;
            val = (T)(val / 256);
        }

        len++;

        for(ushort i = 0; i <= len / 2; i++)
        {
            uchar aux = x[i];
            x[i] = x[len - 1 - i];
            x[x.size() - 1 - i] = aux;
        }
    }

public:
    Num()
    {
        x.resize(0);
        len = 0;
    }
    Num(const Num& other)
    {
        *this = other;
    }

    friend bool operator>(const Num& l, const Num& r)
    {
        for(uchar i = l.len - 1; i >= 0; i--)
        {
            if(l.x[i] != r.x[i])
                return (l.x[i] > r.x[i]);
        }
    }
    friend bool operator<(const Num& l, const Num& r)
    {
        return r > l;
    }
    friend bool operator>=(const Num& l, const Num& r)
    {
        return !(l < r);
    }
    friend bool operator<=(const Num& l, const Num& r)
    {
        return !(l > r);
    }
    friend bool operator==(const Num& l, const Num& r)
    {
        return l >= r && l <= r;
    }
    friend bool operator!=(const Num& l, const Num& r)
    {
        return !(l == r);
    }

    Num& operator=(const string& l)
    {
        string a = l;

        if(a.size() <= 4)
        {
            set<ushort>(a);
        }
        else if(a.size() <= 9)
        {
            set<uint>(a);
        }
        else
        {
            set<ulong>(a);
        }

        return *this;
    }
    Num& operator=(const char* l)
    {
        return (*this = string(l));
    }

    string get()
    {
        if(len == 0)
            return "0";

        if(len <= 2)
        {
            return str<ushort>();
        }
        else if(len <= 4)
        {
            return str<uint>();
        }
        else
        {
            return str<ulong>();
        }
    }

    friend ostream& operator<<(ostream& os, Num& l)
    {
        return os << l.get();
    }
    friend istream& operator>>(istream& is, Num& l)
    {
        string x;
        is >> x;
        l = x;
        return is;
    }
};

int main()
{
    Num x;

    cout << "Entrada: ";
    cin >> x;
    cout << "Saida: "<< x << endl;
    system("pause");

    return 0;
}

结果可能是:

Entrada: 572648319
Saida: 3136023423

0 个答案:

没有答案