Open file, read as ASCII and convert it to HEX in C language?

Hi I’m not the best at C but I’m trying to write a C program that basically opens a text file Which contains ascii text and its working fine now I want that it convert ascii data into HEX and save into different file in HEX format kindly help me how to do It using C programming language ??This is my code so far:

int main()
{

FILE *filePointer;
char ch;


filePointer = fopen("ascii.txt", "r");


if (filePointer == NULL)
{
    printf("File is not available \n");
}
else
{
    
    while ((ch = fgetc(filePointer)) != EOF)
    {
        printf("%c", ch);
    }
}


fclose(filePointer);

return 0;
 }
1 Like

This will work:

#include<stdio.h>
#include<stdlib.h>

int main()
{

FILE *filePointer;
FILE *outputFilePointer;
char ch;


filePointer = fopen("ascii.txt", "r");
outputFilePointer = fopen("hex.txt", "w");


if (filePointer == NULL)
{
    printf("File is not available \n");
}
else
{
    
    while ((ch = fgetc(filePointer)) != EOF)
    {
        fprintf(outputFilePointer, "%02X", ch);
    }
}


fclose(filePointer);
fclose(outputFilePointer);

return 0;
 }