繁体   English   中英

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

[英]Storing hexadecimal values from a std::string in a std::vector and vise versa?

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

怎么样(虽然没有花哨的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);
}

由于我的项目已经依赖于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));

我会选择简单的普通循环

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;
}
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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM