Home > Software engineering >  How to fix hex characters
How to fix hex characters

Time:02-14

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?

CodePudding user response:

getc each byte and print it

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

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

  •  Tags:  
  • c
  • Related