Getting file in hex format - my output vs xxd command output

0 votes

I'm trying to write a simple program to generate hex output from file. This is my two.c file:

#include <stdio.h>

int 
main(void) {
    printf("%s\n", "Hello");
    return 0;
}

which was compiled in this way:

 gcc -std=c99 -Wall -Wextra -pedantic-errors two.c -o two

so I have two executable file.

Now, I wrote a program to read that file and show the binary (hexadecimal) output of it. This is my program:

#include <stdio.h>

int 
fileSize(FILE *ptr) {

    int size = 0;
    fseek(ptr, 0, SEEK_END);
    size = ftell(ptr);
    fseek(ptr, 0, SEEK_SET);
    return size;

}

int 
main(void) {

    FILE *fp;
    fp = fopen("two", "rb");
    int sz = fileSize(fp);
    char buff[ sz ];

    if (!fp) 
        printf("%s\n", "Not great at all");

    else 
        while (!feof(fp)) {
            fgets(buff, sz, fp);
            for (int i = 0; i < sz; i++) {
                printf("%02x%02x ", (buff[i] & 0xFF), (buff[i+1] & 0xFF));
                if (!(i % 8))
                    printf("\n");
            }
            printf("\n");
        }

    fclose(fp);

}

And here is huge output of it http://pastebin.com/RVLy6H9B

The problem is, when I use linux command xxd two > two.hex I'm getting completely different output (it's not about formatting) and there is only about 500 lines of bytes, not about 8k like in my output.

xxd output: http://pastebin.com/Gw9wg93g

Where is the problem? Is something wrong with reading function fgets(buff, sz, fp);?

Apr 20, 2022 in Linux Administration by Edureka
• 13,630 points
850 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Linux Administration

0 votes
0 answers

Linux Command for getting a dump of hex values bytes from Pcap File

I require a Linux command to display ...READ MORE

Apr 13, 2022 in Linux Administration by Rahul
• 9,670 points
286 views
0 votes
0 answers

Linux Command for getting a dump of hex values bytes from Pcap File

I require a linux command to display ...READ MORE

Apr 20, 2022 in Linux Administration by Edureka
• 13,630 points
189 views
0 votes
1 answer

How to collect all output in one file?

You can use 2> to redirect it: foo ...READ MORE

answered Mar 1, 2019 in Linux Administration by ajs3033
• 7,300 points
456 views
0 votes
0 answers

Opening a binary file in VI and XXD

I am attempting to modify the binary ...READ MORE

Apr 13, 2022 in Linux Administration by Rahul
• 9,670 points
487 views
0 votes
0 answers

Linux command to display date in unix format

What command can I use in Linux ...READ MORE

Apr 13, 2022 in Linux Administration by Aditya
• 7,660 points
144 views
0 votes
0 answers

An alternative in php to the xxd command in linux

I need to convert the bin file ...READ MORE

Apr 13, 2022 in Linux Administration by Aditya
• 7,660 points
254 views
0 votes
0 answers
0 votes
0 answers

How to edit a text file in my terminal

I'm using Linux mint and using the ...READ MORE

Apr 25, 2022 in Linux Administration by Edureka
• 13,630 points
64 views
0 votes
1 answer

Is there a limit on the number of words in bash command 'for file in words'?

Anyway, you need to print everything except ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
145 views
0 votes
0 answers

Getting file in hex format - my output vs xxd command output

I'm trying to write a simple program ...READ MORE

Apr 20, 2022 in Linux Administration by Edureka
• 13,630 points
167 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP