Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to fix hex characters

I’m trying to print the content of the image jpg file as hex. However, characters with 0 are not being displayed correctly. For example lets say we have C0 it only shows C.

Here is the code:

#include <stdio.h>
int main(){
    FILE *fp = fopen("steg.jpg", "r");
    int z = 0;
    unsigned char out;
    while(z!=-1){
        
        z=fscanf(fp,"%c", &out);
        if(z!=-1){
            printf("%X ", out);
        }else{
            break;
        }
        
    }
    fclose(fp);
    return 0;
}

What should I do to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

getc each byte and print it

    int z = 0;
    unsigned char out;
    while((z = getc(fp)) != -1){
        printf("%02X ", z);
    }

You’ll want to add a counter and print newlines every so many characters.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading