如何在C++中将大位集即bitset<256>转换为十六进制?

问题描述 投票:0回答:1

我有一个很大的位集<256>,我想将其转换为十六进制,但 to_ulong() 和 to_ullong() 的常见解决方案会引发溢出错误。

        bitset<256> bitresult = to_bitset(hexToBinary(x2)) ^ to_bitset(hexToBinary(random[i-1]));
            stringstream res;

            bitset<64> b1;
            bitset<64> b2;
            bitset<64> b3;
            bitset<64> b4;

            for (int i1=0; i1<64; i1++)
            b1[i1]=bitresult[i1];
            for (int i1=0; i1<64; i1++)
            b2[i1]=bitresult[i1+64];
            for (int i1=0; i1<64; i1++)
            b3[i1]=bitresult[i1+128];
            for (int i1=0; i1<64; i1++)
            b4[i1]=bitresult[i1+192];

            res<< hex << b1.to_ullong();
            res<< hex << b2.to_ullong();
            res<< hex << b3.to_ullong();
            res<< hex << b4.to_ullong();
            string hashresult = res.str();
            cout << hashresult <<endl;
                 return sha256(hashresult);

但是,由于尺寸较大,我在将它们返回到十六进制时遇到问题, to.ullong() 会引发溢出错误。上面的代码是我的尝试,但对我来说似乎非常有缺陷,有更好的解决方案吗?

c++ binary hex bitset
1个回答
0
投票

供参考,请参阅:这个答案

您只需为流设置

hex
修饰符即可。

bitset<256> bitresult = to_bitset(hexToBinary(x2)) ^ to_bitset(hexToBinary(random[i-1]));

stringstream res;
res << hex << bitresult;

string hashresult = res.str();

cout << hashresult << endl;

return sha256(hashresult);
© www.soinside.com 2019 - 2024. All rights reserved.