Thread: Print hexadecimal bin file

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    5

    Print hexadecimal bin file

    Hello.

    I have a question. I need print my bin file in hexadecimal. I have function

    Code:
    void ReadFile(char *name){
        FILE *file;
        char *buffer;
        unsigned long fileLen;
    
        //open file
        file = fopen(name, "rb");
        if (!file)
        {
            fprintf(stderr, "Unable to open file %s", name);
            return;
        }
    
        //Size of file?
        fseek(file, 0, SEEK_END);
        fileLen=ftell(file);
        fseek(file, 0, SEEK_SET);
    
            //print size of my file
        fprintf(stderr, "\nSize of myfile.txt: %ld bytes.\n",fileLen);
    
        //Allocate memory
        buffer=(char *)malloc(fileLen+1);
        if (!buffer)
        {
            fprintf(stderr, "Memory error!");
            fclose(file);
            return;
        }
    
        //Read file contents into buffer
        fread(buffer, fileLen, 1, file);
        fclose(file);
    
        //Do what ever with buffer
        for(int i = 0; i < sizeof( buffer ); i++ )
        {
            if( i % 16 == 0 )
            {
                fprintf(stderr, "\n%08X: ", i );
            }
            fprintf(stderr, "%02X ", buffer[i] );
        }
    
    
        free(buffer);
    }
    And I need print complete my bin file. Now, my output is:
    Code:
    00000000: 00 00 02 20 05 1F 00 08
    In file, you delete .zip
    EMPTY.bin.zip

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    Use filelen rather than sizeof buffer in your loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2018
    Posts
    5
    Ok, I modifiy this function.

    Code:
    //Do what ever with buffer
        for(int i = 0; i < fileLen; i++ )
        {
            if( i % 16 == 0 )
            {
                fprintf(stderr, "\n%08X: ", i );
            }
            fprintf(stderr, "%02X ", buffer[i] );
        }
    But my otput is:
    Code:
    00000000: 00 00 02 20 05 1F 00 08 FFFFFFE5 01 00 08 FFFFFFE9 01 00 08 
    00000010: FFFFFFED 01 00 08 FFFFFFF1 01 00 08 FFFFFFF5 01 00 08 00 00 00 00 
    00000020: 00 00 00 00 00 00 00 00 00 00 00 00 FFFFFFF9 01 00 08 
    00000030: FFFFFFFD 01 00 08 00 00 00 00 01 02 00 08 05 02 00 08 
    00000040: 55 1F 00 08 55 1F 00 08 55 1F 00 08 55 1F 00 08 
    00000050: 55 1F 00 08 55 1F 00 08 59 12 00 08 71 12 00 08 
    00000060: FFFFFF89 12 00 08 FFFFFFA1 12 00 08 FFFFFFB9 12 00 08 55 1F 00 08 
    00000070: 55 1F 00 08 55 1F 00 08 55 1F 00 08 55 1F 00 08 
    00000080: 55 1F 00 08 55 1F 00 08 55 1F 00 08 55 1F 00 08 
    00000090: 55 1F 00 08 55 1F 00 08 55 1F 00 08 FFFFFFD1 12 00 08
    And now I have a problem. You can see output, but there are any problem sectors "FFFFFF"-
    Last edited by jirkaj4; 01-22-2018 at 03:24 AM.

  4. #4
    Registered User
    Join Date
    Jan 2018
    Posts
    5
    Problem was in print function.

    Code:
    for(int i = 0; i < fileLen; i++ )
        {
            if( i % 16 == 0 )
            {
                fprintf(stderr, "\n%08X: ", i );
            }
            fprintf(stderr, "%02X ", (int)(*(unsigned char*)(&buffer[i])) );
        }

    And now is output:
    Code:
    00000000: 00 00 02 20 05 1F 00 08 E5 01 00 08 E9 01 00 08 
    00000010: ED 01 00 08 F1 01 00 08 F5 01 00 08 00 00 00 00 
    00000020: 00 00 00 00 00 00 00 00 00 00 00 00 F9 01 00 08 
    00000030: FD 01 00 08 00 00 00 00 01 02 00 08 05 02 00 08 
    00000040: 55 1F 00 08 55 1F 00 08 55 1F 00 08 55 1F 00 08 
    00000050: 55 1F 00 08 55 1F 00 08 59 12 00 08 71 12 00 08 
    00000060: 89 12 00 08 A1 12 00 08 B9 12 00 08 55 1F 00 08 
    .......
    00001FB0: F8 B5 00 BF F8 BC 08 BC 9E 46 70 47 00 00 00 00 
    00001FC0: 00 00 00 00 01 02 03 04 06 07 08 09 00 00 00 00 
    00001FD0: 01 02 03 04 4D 4C 00 00 AD 01 00 08 89 01 00 08 
    00001FE0: 01 00 00 00 00 24 F4 00
    Last edited by jirkaj4; 01-22-2018 at 03:43 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,436
    All you had to do was make your buffer unsigned char* to begin with.
    Then you can remove that cast mess.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to print 64 bit unsigned long long in hexadecimal
    By kalai in forum C++ Programming
    Replies: 6
    Last Post: 08-08-2016, 01:55 AM
  2. Print hexadecimal strings from binary
    By Mestertik in forum C++ Programming
    Replies: 8
    Last Post: 08-21-2013, 04:28 AM
  3. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  4. Print Hexadecimal Numbers Of a File
    By nathanpc in forum C Programming
    Replies: 4
    Last Post: 01-13-2010, 11:19 PM
  5. Hexadecimal read of a file
    By SFilip in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 04:19 PM

Tags for this Thread