-5

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:

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

This is what i need:

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

I found function sscanf() that could solve my problem but i dont know how to properly use it onmy input array.

How can I achive this conversion ?

6
  • 2
    possible duplicate of How to convert hex string to char array of hex in C/C++ Commented Mar 26, 2015 at 8:48
  • 1
    Your second array should have only 512 elements, right? Commented Mar 26, 2015 at 8:48
  • 1
    Please show us your code so that we can see what you have tried so far. Commented Mar 26, 2015 at 8:48
  • 4
    Shouldn't hexval be an unsigned short[512] array? (and text[1025] for null byte?) Commented Mar 26, 2015 at 8:49
  • size of array does not really matter, it is intentionally larger Commented Mar 26, 2015 at 8:52

2 Answers 2

1

Some easy implementations

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

In this case the result is the half size of the input. Two chars are leading to one value in the result. If this is a time critical routine you may write your own conversion from two chars in a number.

Sign up to request clarification or add additional context in comments.

Comments

0

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 <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("");

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.