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

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

Options
  • 25-03-2008 11: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 Posts: 7,468 ✭✭✭Evil Phil


    unstoppable :pac:


Advertisement