I am using C language to encrypt a text file and decrypt it by the means of an AES Algorithm. This algorithm can only encrypts characters if they are stated in the format of an unsigned character where a letter A is represented by 0x41.
what i am trying to do is to read the text from the file, store it in an array of characters (of size 16 character) and then convert each character to its hexadecimal notation.
I am facing a problem in storing this notation in an unsigned characters array where i cant find the solution to store each string (0x41) in an element of the unsigned characters.
i am using the following snap of code to do the above and i hope you can grant me help to do this.

static uint8_t TextToEncrypt[16]; // For 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]); // convert each character of the array of characters to the hexadecimal notation and store it in the TempBuffer
            // 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
        }
    }

So, what is exactly your problem? You need to make the TempBuffer bigger than 4 chars - you have no room for a terminating null char ('\0') so you are creating a buffer overflow.

I will give an example ,

Assume the text in file is: "Abcd";

after reading file , i convert each character to its hexa decimal notation ,

like: hex. not. of char 'A' is '0x41' and so on. This is working fine

then i want to add this notation to an array of unsigned char but Idont know how and this is my problem!!!

At the end the array of unsigned char should look like this:
uint8_t = ['0x41','0x42','0x31','0x51','0x46',........];

Edited 8 Months Ago by alegatross

Well, the string "Abcd" would be 0x41, 0x62, 0x63, 0x64, and 0x00 (terminating null char) - caps != lower-case...

rubberman , thanks for your help,
I need the code which fills the unsigned char array with these notations . How to cast 0x41 to unsigned char?

thanks

This article has been dead for over six months. Start a new discussion instead.