Q:

C program to convert String into Hexadecimal

0

C program to convert String into Hexadecimal

In this program we will read a String and convert the string into Hexadecimal String. We will convert each character of the string in it’s equivalent hexadecimal value and insert the converted value in a string and finally print the Hexadecimal String.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Converting String into Hexadecimal String using C program

/*C program to convert String into Hexadecimal.*/

#include <stdio.h>
#include <string.h>

int main()
{
    unsigned char str[100], strH[200];
    int i, j;

    printf("Enter string: ");
    scanf("%[^\n]s", str);

    printf("\nString is: %s\n", str);

    /*set strH with nulls*/
    memset(strH, 0, sizeof(strH));

    /*converting str character into Hex and adding into strH*/
    for (i = 0, j = 0; i < strlen(str); i++, j += 2) {
        sprintf((char*)strH + j, "%02X", str[i]);
    }
    strH[j] = '\0'; /*adding NULL in the end*/

    printf("Hexadecimal converted string is: \n");
    printf("%s\n", strH);

    return 0;
}

Output:

Enter string: Hello world, This is my first program.

String is: Hello world, This is my first program.
Hexadecimal converted string is: 
48656C6C6F20776F726C642C2054686973206973206D792066697273742070726F6772616D2E

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C language important programs ( Advance Programs )

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to check given string is a valid IPv4 ad... >>
<< C program to demonstrate example of Variable Argum...