2

16 進数の文字列があり、これを 16 進数の符号なし char 配列に変換したい!

std::string hex = "0c45a1bf"

unsigned char hexCh = ""
    [0] = "0c"
    [1] = "45"
    [2] = "a1"
    [3] = "bf"

この動作を hexCh で表示したい!

stringstream と std::hex に対する最良の方法は? 実装はありますか?!

どうも

4

5 に答える 5

4

16 進文字列の各ペアのが必要であると仮定します。

std::string hex = "0c45a1bf";

int main(int argc, char **argv)
{
    union U
    {
        unsigned int value;
        unsigned char components[4];
    };

    U u;

    std::stringstream SS(hex);
    SS >> std::hex >> u.value;

    std::cout << u.components[0] << '\n'; // the 0c value
    std::cout << u.components[1] << '\n'; // the 45 value
    std::cout << u.components[2] << '\n'; // the a1 value
    std::cout << u.components[3] << '\n'; // the bf value
}

値を に読み込み、union各サブパーツを取得できます。

于 2012-11-21T10:33:22.247 に答える
4

std::stringstream+を使用std::hex:

std::stringstream ss;
std::string hex = "0c45a1bf";
std::vector<unsigned char> hexCh;
unsigned int buffer;
int offset = 0;
while (offset < hex.length()) {
   ss.clear();
   ss << std::hex << hex.substr(offset, 2);
   ss >> buffer;
   hexCh.push_back(static_cast<unsigned char>(buffer));
   offset += 2;
}
于 2012-11-21T10:33:27.550 に答える
0

考えられる解決策:(thx Denis Ermolin):

void ClassA::FuncA(unsigned char *answer)
{
    std::string hex = "0c45a1bf";
    std::stringstream convertStream;

    // if you have something like "0c 45 a1 bf" -> delete blanks
    hex.erase( std::remove(hex.begin(), hex.end(), ' '), hex.end() );

    int offset = 0, i = 0;      
    while (offset < hex.length()) 
    {
        unsigned int buffer;

        convertStream << std::hex << hex.substr(offset, 2);         
        convertStream >> std::hex >> buffer;

        answer[i] = static_cast<unsigned char>(buffer);

        offset += 2;
        i++;

        // empty the stringstream
        convertStream.str(std::string());
        convertStream.clear();
    }
}
于 2012-11-23T12:18:04.980 に答える
0

この 2 つの関数を一緒に使用すると、次のように機能します。

これは簡単です:

inline int char2hex(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 std::runtime_error("wrong char");
}

これはもう少し複雑です:

std::vector<unsigned char> str2hex(const std::string& hexStr)
{
     std::vector<unsigned char> retVal;
     bool highPart = ((hexStr.length() % 2) == 0);  
     // for odd number of characters - we add an extra 0 for the first one:
     if (!highPart)
         retVal.push_back(0);
     std::for_each(hexStr.begin(), hexStr.end(), 
        [&](char nextChar) {
           if (highPart) 
               // this is first char for the given hex number:
               retVal.push_back(0x10 * char2hex(nextChar));
           else
              // this is the second char for the given hex number
              retVal.back() += char2hex(nextChar);
           highPart = !highPart;
       }
     );

    return retVal;
}

そして、それが機能する:

int main() {
  std::string someHex =  "c45a1bf";
  std::vector<unsigned char> someUHex = str2hex(someHex);
  std::copy(someUHex.begin(), someUHex.end(), std::ostream_iterator<int>(std::cout << std::hex, ""));
}
于 2012-11-21T12:05:39.720 に答える