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 in Linux Administration by Edureka
• 9,320 points
11 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 in Linux Administration by Rahul
• 8,980 points
19 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 in Linux Administration by Edureka
• 9,320 points
8 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,280 points
269 views
0 votes
0 answers

Opening a binary file in VI and XXD

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

Apr 13 in Linux Administration by Rahul
• 8,980 points
22 views
0 votes
0 answers

Linux command to display date in unix format

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

Apr 13 in Linux Administration by Aditya
• 7,280 points
20 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 in Linux Administration by Aditya
• 7,280 points
27 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 in Linux Administration by Edureka
• 9,320 points
7 views
–1 vote
1 answer

How to get octal file permission in linux?

You can use this: stat -c "%a %n" ...READ MORE

answered Jan 3, 2019 in Linux Administration by Omkar
• 69,190 points
975 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 in Linux Administration by Edureka
• 9,320 points
39 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP