-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 ?

Pastx
  • 687
  • 3
  • 8
  • 23
  • 2
    possible duplicate of [How to convert hex string to char array of hex in C/C++](http://stackoverflow.com/questions/26411447/how-to-convert-hex-string-to-char-array-of-hex-in-c-c) – Paul Hankin Mar 26 '15 at 08:48
  • 1
    Your second array should have only 512 elements, right? – samgak Mar 26 '15 at 08:48
  • 1
    Please [show us your code](http://stackoverflow.com/help/mcve) so that we can see [what you have tried so far](http://whathaveyoutried.com). – GoBusto Mar 26 '15 at 08:48
  • 4
    Shouldn't hexval be an `unsigned short[512]` array? (and `text[1025]` for null byte?) – axelduch Mar 26 '15 at 08:49
  • size of array does not really matter, it is intentionally larger – Pastx Mar 26 '15 at 08:52
  • Just loop through by character and process hexval[i] concatenated with hexval[i+1] with a step size of 2. – sabbahillel Mar 26 '15 at 09:30

2 Answers2

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.

Tunichtgut
  • 301
  • 1
  • 6
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("");

    }

}
sixleaves
  • 1
  • 1