Convert char array to hex array (C++)

前端 未结 2 1466
[愿得一人]
[愿得一人] 2021-01-29 16:17

My problem is converting array of chars to array of hexadecimal numbers, i need to take 2chars from char array and conver them into one hex number.

This is my input:

2条回答
  •  自闭症患者
    2021-01-29 17:11

    en,my english is bad,so i write the code hope to explain what i think. personally speacking i think we can write a function for this question.

    #include 
    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    map 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 << " "<

提交回复
热议问题