简体   繁体   English

fprintf一个无符号的字符,如字符

[英]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() 但是由于我的程序在输入文件上使用了getLine(),因此具有AAAA\\x00\\x01BBBB将被视为'A','A','A','A','\\','x','0','0',...使getLine()与memcpy()等效的方法有哪些?

Just use the %lu token to print an unsigned long : 只需使用%lu令牌打印一个unsigned long

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

If you want it in hex: 如果要用十六进制表示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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