Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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