简体   繁体   中英

Convert a string of characters to an unsigned Character in C

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM