在std :: vector中存储std :: string的十六进制值,反之亦然?

时间:2011-01-30 14:20:44

标签: c++ string stl vector

假设你有一个std::string("DEADBEEF"),那么在std :: vector中存储每两个值的最优雅的方法是什么(如果可能的话,很可能是std::vector<unsigned char>)所以{ {1}}看起来有点像std::vector(如果是数组)?什么是最好的撤消方式({ 0xDE, 0xAD, 0xBE, 0xEF } - &gt; std::vector<unsigned char>)。

4 个答案:

答案 0 :(得分:2)

怎么样(虽然没有花哨的stl杂耍):

#include <string>
#include <vector>

// exception thrown by the encoder
class DecodeError {
public:
    DecodeError() {}
    DecodeError(const char* s): msg(s) {}
    DecodeError(const std::string& s): msg(s) {}
    const char* what() { return msg.c_str(); }
private:
    std::string msg;
};

class HexEncoder {
public:
    void decode(const std::string& str, std::vector<unsigned char>& v);
private:
    unsigned char decode(char);
};

void HexEncoder::decode(const std::string& s, std::vector<unsigned char>& v)
{
    if (s.size() % 2 != 0)
        throw DecodeError("invalid string length");

    v.reserve(s.size() / 2);
    std::string::const_iterator it=s.begin();
    while (it != s.end()) {
        unsigned char nibble1 = decode(*it++);
        unsigned char nibble2 = decode(*it++);
        v.push_back(nibble1 << 4 + nibble2);
    }
}

unsigned char HexEncoder::decode(char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    if (c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    if (c >= 'a' && c <= 'f')
        return c - 'a' + 10;
    throw DecodeError("invalid character");
}

int main()
{
    std::string s("DEADBEEF");

    std::vector<unsigned char> v;

    HexEncoder enc;
    enc.decode(s, v);
}

答案 1 :(得分:1)

我会选择简单的普通循环

int hexValue(int c)
{
    if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    return -1;
}

std::vector<unsigned char> toBin(const std::string& s)
{
    std::vector<unsigned char> result;
    for (int i=0,n=s.size(); i<n-1; i+=2)
    {
        int x1 = hexValue(s[i]);
        int x2 = hexValue(s[i+1]);
        if (x1 >= 0 && x2 >= 0)
            result.push_back(x1*16 + x2);
    }
    return result;
}

std::string toHex(const std::vector<unsigned char>buf)
{
    const char *hex = "0123456789ABCDEF";
    int n = buf.size();
    std::string result(n*2, ' ');
    for (int i=0; i<n; i++)
    {
        result[i*2] = hex[buf[i]>>4];
        result[i*2+1] = hex[buf[i]&15];
    }
    return result;
}

如果你需要这个转换非常快(假设输入足够大且正式正确)......

std::vector<unsigned char> toBin(const std::string& s)
{
    static unsigned char H[256], L[256];
    if (L['1'] == 0)
    {
        for (int i=0; i<10; i++)
        {
            H['0'+i] = (i<<4);
            L['0'+i] = i;
        }
        for (int i=0; i<6; i++)
        {
            H['a'+i] = H['A'+i] = ((10+i)<<4);
            L['a'+i] = L['A'+i] = (10+i);
        }
    }
    std::vector<unsigned char> result(s.size()>>1);
    for (int i=0,n=s.size(); i<n-1; i+=2)
      result[i>>1] = H[s[i]]+L[s[i+1]];
    return result;
}

答案 2 :(得分:1)

for(std::iterator<string> it = str.begin(); it != str.end(); std::advance(it, 2))
{
    char tmp;
    std::copy(it, it + 2, tmp);
    scanf("%02X", &tmp);
    vect.push_back(tmp);
}

这是转换的粗略草稿,但我不确定它是否正常工作。

答案 3 :(得分:1)

由于我的项目已经取决于boost,我会使用boost/algorithm/hex.hpp

#include <string>
#include <vector>
#include <iterator>

#include <boost/algorithm/hex.hpp>

...

string stringValue("DEADBEEF");
vector<unsigned char> vectorValue;

字符串到十六进制转换:

boost::algorithm::unhex(stringValue, back_inserter(vectorValue));

对于十六进制到字符串的转换:

boost::algorithm::hex(vectorValue, back_inserter(stringValue));