-3

I am searching for a way to convert a string of characters into a string of their hexadecimal representations. So the letter A will be represented as 0x41.

What I am trying to do is encrypting a text file using an algorithm that encrypts hexadecimal characters of notation 0x** where ** resembles the hexadecimal notation of the character.

I am reading an array of 16 characters from the file to an array of characters, then I should convert them to hexadecimal notation so I can pass them to the encryption function.

I am using the following snippet of code to convert the array of characters to hexadecimal. I created a TempBuffer to hold the hexadecimal value for each character so it will be in the notation 0x**. My problem is how to store the value in TempBuffer to an element of the Unsigned characters array. Look the code below:

static uint8_t TextToEncrypt[16]; // Declaring the array to store the hexadecimal notation

void ToHex(char InText[]) // The Function to Convert an array of characters to hexadecimal array
{
    int i=0;
    for(i=0; i<16; i++)
    {
        char TempBuffer[4]; // Tempbuffer to hold the converted value 
        sprintf(TempBuffer,"0x%x",InText[i]); // converting to hexadecimal
        // Here i need to store the value in TempBuffer which will be something like 0x41 in TextToEncrypt[i] so I can pass it to encryption
    }
}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • I tried a lot, but without any result, i prefer to have something guaranteed to test it. i cant post all what i tried – Mohammad M Moshawrab Mar 16 '16 at 18:51
  • I think there is some confusion about what a "hexadecimal array " is. Perhaps you need to step back to the real problem, your encryption code. – zaph Mar 16 '16 at 20:52
  • Your question has been asked and aswered hundreds of times over. You should do some research, just dump your title into a websearch and read the first few results. – Ulrich Eckhardt Mar 16 '16 at 21:08
  • Consider accepting answers that are helpful. To accept an answer click on the hollow checkmark next to the answer that is best, doing so will increase your reputation and allow more capabilities, See [reputation faq](http://stackoverflow.com/faq#reputation) See [this page](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work) for more detail. – zaph Mar 17 '16 at 14:11
  • i hope i could clarify my question. i rephrased it. Thanks all for your cooperation – Mohammad M Moshawrab Mar 17 '16 at 18:14

2 Answers2

0

May be what you need is atoi to convert your string to integer, and then some way to convert that integer in decimal to hexadecimal. Take a look this SO post. To convert to hex, I prefer to use sprintf.

Converting string to decimal:
char decstr[] = "1202";
int  decimal = atoi(decstr);
printf("%d\n", decimal);

// prints 1202
Converting decimal to hexadecimal:
char hex[5];
sprintf(hex, "%x", decimal);
printf("%s\n", hex);

// prints 4b2

Edited after OP added an example

As each hexadecimal representation in your case will have length of four, you need to define a 2d uint8_t array. Take a look below:

uint8_t TextToEncrypt[16][4];

void ToHex(char InText[])
{
    int i, j;
    char TempBuffer[4];

    for(i=0; i<16; i++)
    {
        sprintf(TempBuffer,"0x%x", InText[i]);
        for (j = 0; j < 4; ++j)
            TextToEncrypt[i][j] = TempBuffer[j];
    }

    for (i = 0; i < 16; ++i)
    {
        for(j=0; j<4; ++j)
            printf("%c", TextToEncrypt[i][j]);
        printf("\n");
    }
}

Remember to include the header stdint.h, if you are using MinGW compiler

Community
  • 1
  • 1
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
  • it may seem weird. but i am trying first to convert each character to hexademical representation through this: sprintf(TempBuffer,"0x%x",InText[i]); then i want to store the data of temp buffer which will be as 0x54 or 0x78 or something like this i want to store it in an unsigned character of type uint8_t which defined to be of this format. excuse my questions. – Mohammad M Moshawrab Mar 16 '16 at 19:44
  • Thanks for your advises. i rephrased the question all again. and i deleted the answer i had posted previously. hope i can find a solution for the point i stated above – Mohammad M Moshawrab Mar 17 '16 at 18:15
  • @MohammadMMoshawrab Look at the edited answer. Hope that helps, finally! – Sнаđошƒаӽ Mar 17 '16 at 18:38
  • we are a little bit far from the solution. the edit you put is very good except that the type of the array i will store the hexadecimal in is not a characters array but of type uint8_t and it can't be used in the strcpy() function.. that what is making it confusing. if it was storing in another array of characters then it will be easily handled. but unfortunately it is not the case. – Mohammad M Moshawrab Mar 17 '16 at 18:44
  • it seems i am not understanding the concept of uint8_t..can some body grant some help in this. i am not a C expert. – Mohammad M Moshawrab Mar 17 '16 at 19:06
0

I still don't know exactly what you want, here is another try.

This is just to give you an idea, there are still lots of problems such as handling bad input and doing something with outText:

void toHex(char InText[])
{
    static int outInts[17];

    for(int i=0; i<16; i++)
    {
        outInts[i] = InText[i];
    }
    for (int i=0; i<16; i++) {
        printf("%i, ", outInts[i]);
    }
    printf("\n");
}
    toHex("abcdefghijklmnopq");

Output:

97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112

If this is not what you want you need to provide an example.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
zaph
  • 111,848
  • 21
  • 189
  • 228
  • thanks a lot but my problem is to store the converted text in an array of unsigned integer and not in a character array. i can't encrypt an array of characters based on the algorithm i am using... any ideas – Mohammad M Moshawrab Mar 16 '16 at 20:31