Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to print binary file as hexadecimal characters in C

1 Answer

0 votes
#include <stdio.h> 
 
int main(void)
{
    FILE* fp = fopen("d:\\data.bin", "rb");
    
    if (fp) 
    { 
        char ch, i = 1;
        
        while ((ch = fgetc(fp)) != EOF) 
        {
                (i++ == 10) ? printf(" %02X\n", ch) : printf(" %02X", ch);
                if (i > 10) i = 1; // i is just for print 10 results on a line
        }
            
        fclose(fp);
    }
 
    
    return 0;
}


/*
run:
   
 18 00 00 00 2D 00 00 00 5E 00
 00 00 29 00 00 00 46 00 00 00
 49 00 00 00 1A 00 00 00 3C 00
 00 00 24 00 00 00 3A 00 00 00

*/


answered Mar 1, 2015 by avibootz

Related questions

1 answer 88 views
1 answer 151 views
151 views asked Sep 18, 2021 by avibootz
1 answer 154 views
154 views asked Sep 18, 2021 by avibootz
1 answer 125 views
125 views asked Sep 18, 2021 by avibootz
1 answer 124 views
124 views asked Sep 17, 2021 by avibootz
1 answer 121 views
121 views asked Aug 24, 2021 by avibootz
1 answer 171 views
...