【问题标题】:Convert char array to hex array (C++)将字符数组转换为十六进制数组(C++)
【发布时间】:2015-03-26 08:45:04
【问题描述】:

我的问题是将字符数组转换为十六进制数数组,我需要从字符数组中取出 2 个字符并将它们转换为一个十六进制数。

这是我的输入:

unsigned char text [1024]= "06fb7405eba8d9e94fb1f28f0dd21fdec55fd54750ee84d95ecccf2b1b48";

这是我需要的:

unsigned char hexval[1024] = {0x06, 0xfb, 0x74, 0x05, 0xeb, 0xa8, 0xd9, 0xe9, 0x4f, 0xb1, 0xf2, 0x8f, 0x0d, 0xd2, 0x1f, 0xde, 0xc5, 0x5f, 0xd5, 0x47, 0x50, 0xee, 0x84, 0xd9, 0x5e, 0xcc, 0xcf, 0x2b, 0x1b, 0x48};

我找到了可以解决我的问题的函数sscanf(),但我不知道如何在我的输入数组上正确使用它。

我怎样才能实现这种转换?

【问题讨论】:

标签: c++


【解决方案1】:

一些简单的实现

unsigned char text[1024] = "06fb7405eba8d9e94fb1f28f0dd21fdec55fd54750ee84d95ecccf2b1b48";
unsigned char result[512];

int size = 60;

assert(size % 2 == 0);

for (int i = 0; i + 1 < size; i += 2)
{
    std::string s(text + i, text + i + 2);
    auto x = std::stoi(s, 0, 16);
    result[i / 2] = x;
}
// or
for (int i = 0; i + 1 < size; i += 2)
{
    unsigned char c1 = *(text + i);
    unsigned char c2 = *(text + i + 1);
    char buffer[] = { c1, c2, 0 };
    auto x = std::strtol(buffer, 0, 16);
    result[i / 2] = x;
}

在这种情况下,结果是输入的一半大小。结果中的两个字符导致一个值。 如果这是一个时间紧迫的例程,您可以编写自己的数字中的两个字符的转换。

【讨论】:

    【解决方案2】:

    en,我的英语不好,所以我写代码希望能解释我的想法。 就个人而言,我认为我们可以为这个问题编写一个函数。

    #include <iostream>
    #include <map>
    #include <cstring>
    #include <string>
    #include <cstdio>
    
    using namespace std;
    
    map<string, int> convertTable;
    void initHexTable();
    void convertToHex(unsigned char * text, unsigned char * hexval, int &i);
    void print(unsigned char *p, int len);
    
    char hexIndex[] = {'0','1','2','3','4','5','6','7','8','9','a', 'b', 'c', 'd', 'e', 'f'};
    int main() {
    
        unsigned char text[1024]= "06fb7405eba8d9e94fb1f28f0dd21fdec55fd54750ee84d95ecccf2b1b48";
        unsigned char hexval[1024];
        int len = 0;
        initHexTable();
        convertToHex(text, hexval, len);
        print(hexval, len);
        return 0;
    }
    
    void initHexTable() {
    
        for (int i = 0; i < 16; i++) {
    
            for (int j = 0; j < 16; j++) {
                 string key;
                 key += hexIndex[i]; key += hexIndex[j];
                 //cout << key << " ";
                 convertTable[key] = i * 16 + j;
    
            }
    
        }
    }
    
    void convertToHex(unsigned char * text, unsigned char * hexval, int &i) {
    
        string s((char *)text);
        string key;
        int str_size = strlen((char*)text);
        //cout << "str_size = " << str_size;
        int start = 0, len = 2;
        for (;;) {
            if (start >= str_size) break;
            key = s.substr(start, len);
            //cout << " "<<convertTable[key]<< " ";
            hexval[i++] = convertTable[key];
            start += 2;
    
    
        }
    }
    
    void print(unsigned char *p, int len) {
    
        for (int i = 0; i < len; i++) {
    
            printf("0X%03d ", p[i]);
            if (!((i + 1) % 4)) puts("");
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 2017-06-17
      • 2022-01-09
      • 2022-01-09
      • 1970-01-01
      相关资源
      最近更新 更多