Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Read/Write hex from/to a file in C?

  • 25-03-2008 10:14pm
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I've googled this but I'm not getting much joy.

    Does anyone know of an easy way to read/write hex from/to a file in C?

    Thank you.


Comments

  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    OK I have reading in hex working, but writing out hex?
    void read(char *filename, unsigned char *message) {
    
    		FILE *fp;
    
    		fp=fopen(filename, "r");
    
    		int i = 0;
    
    		while(!feof(fp))  {
          			fscanf(fp, "%x", &message[i]);
          			i++;
    		} 
    
    		fclose(fp);
    }
    


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Ah got it.
    void write(char *filename, unsigned char *message) {
    
    		FILE *fp;
    
    		fp=fopen(filename, "w");
    
    		// i know i'm only going to write 8 bytes
    		for(int x = 0; x <8; x++)
          			fprintf(fp, "&#37;x", message[x]);
    
    		fclose(fp);
    }
    


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    unstoppable :pac:


Advertisement