Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
 }


What I have tried:

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
Posted
Updated 7-Jun-21 5:32am

Use the %X format type in your printf statement: printf - C++ Reference[^]
 
Share this answer
 
Comments
CPallini 7-Jun-21 11:39am    
5.
You know, there is not an 'hexadecimal format' for a file. However, there could be a file containing the hexadecimal representation of binary (as well ASCII) data.
To obtain such a file replace (as already suggested by Richard)
Quote:
printf("%c", ch);

with
C
printf("%02X ", ch);
 
Share this answer
 
Comments
Richard MacCutchan 7-Jun-21 11:41am    
Have a 5 from me. I omitted the width specification in the (rather forlorn) hope that OP would actually study the documentation in detail.
CPallini 7-Jun-21 16:31pm    
Thank you, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900