简体   繁体   中英

fprintf an unsigned long as a char

I want to do something like:

unsigned long addr = 0x000decaf;
...
fprintf(fp, "AAAAAAAAAAAAAAAAAA$s", addr);

Normally I would do

unsigned long addr = 0x000decaf;
memcpy(buf, &addr, 4);

But since my program uses getLine() on an input file so having something like AAAA\\x00\\x01BBBB will get treated as 'A','A','A','A','\\','x','0','0',... what are some of the tricks I can do to make getLine() to do the equivalence of what I am trying to do with memcpy()

Just use the %lu token to print an unsigned long :

fprintf(fp, "%lu", addr);

If you want it in hex:

fprintf(fp, "0x%x", addr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM