Thread: how to write a hex value to file ?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    how to write a hex value to file ?

    how to write a hex value to file ?
    Code:
    void samplecode(void){ 
    
    FILE *fp;
    char buff[8];
    
    fp=fopen("tmp.bin","r+w+b");
    
    buff[0]='e';
    buff[1]='f';
    buff[2]='a';
    buff[3]='4';
    buff[4]='9';
    buff[5]='b';
    buff[6]='4';
    buff[7]='d';
    
    fwrite(buff,1,8,fp); //??????????????????
    
    fclose(fp);
    }

    I want to write 'ef' hex value to tmp.bin with buff but I can't know how do it .
    i want to write "efa49b4d" hex value to tmp.bin .

    Thanks for helps

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    char *buff = "efa49b4d";
    fprintf(f,"%s", buff);
    Code:
    unsigned int t = 0xefa49b4d;
    fprintf(f,"%08x",t);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Or even:

    Code:
    char buff[4];
    buff[0]=0xef;
    buff[1]=0xa4;
    buff[2]=0x9b;
    buff[3]=0x4d;

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    unfortunatlly the values Buff[8] calculated with one library QuickCrc from SlavaSoft Inc.

    i must convert it and then write to file .

    thhanks

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by noobe View Post
    unfortunatlly the values Buff[8] calculated with one library QuickCrc from SlavaSoft Inc.

    i must convert it and then write to file .

    thhanks
    So? what is the problem? "%8.8s" format of fprintf should work fine
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    3
    really i want write "ef" in one byte but with %s save e in one byte and f in one byte with saving assci cods . assci code of "e" saved . and so on .

    I want save the string with8 ellement in 4 byte with convering every 2 ellement in one hex byte .

    do you konw my problem ?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I was trying to think of a library function to do this, but couldn't really think of any. Perhaps something like this would work for you:

    Code:
    size_t hex_string_to_hex_data( char const* txt, void* dst, size_t max )
    {
    	unsigned char
    		nyb,
    		* cvt = ( unsigned char* )dst;
    	size_t
    		cnt,
    		idx, 	
    		len = strlen( txt ), 
    		hlf = len / 2;
    	if( len == 0 || len % 2 != 0 || hlf > max )
    		return 0;
    	for( idx = 0; idx < len; idx += 2, ++cvt )
    	{
    		*cvt = 0;
    		for( cnt = 0; cnt < 2; ++cnt )
    		{
    			nyb = tolower( txt[ idx + cnt ] );
    			if( isdigit( nyb ) )
    				nyb -= '0';
    			else if( nyb >= 'a' && nyb <= 'f' )
    				nyb = ( nyb - 'a' ) + 10;
    			else 
    				return 0;	
    			*cvt |= nyb << ( ( 1 - cnt ) << 2 );
    		}
    	}
    	return hlf;
    }

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,135
    Use strtol() or strtoul() to convert a hex string to a number. Then use fputc() or fwrite() to write the numbers to a file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SYnchronize read & write of a cache file
    By lei_michelle in forum C Programming
    Replies: 4
    Last Post: 02-26-2008, 05:49 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM