【发布时间】:2014-04-09 15:21:46
【问题描述】:
这是我的问题,我有一个如下所示的字节结构:
struct machinecode{
char byte1[2];
char byte2[2];
char byte3[2];
char byte4[2];
char byte5[2];
char byte6[2];
char byte7[2];
char byte8[2];
char byte9[2];
};
struct machinecode WRITEME[500];
现在,这些 2 字节字符或字符串的集合被格式化为十六进制字节,例如:
byte1 = "01"
byte2 = "C0"
char 字节的分配方式如下:
...
char * returner
...
strncpy(WRITEME[index].byte1, "00", 2);
strncpy(WRITEME[index].byte2, returner, 2);
我的打印代码如下所示:
while(counter < max){
...
else if(prog_counter[counter2] == 2){
fprintf(w, ???, WRITEME[counter].byte1);
fprintf(w, ???, WRITEME[counter].byte2);
}
...
}
现在我希望将字符串打印为十六进制字节,我需要为 fprintf 使用哪种格式(???)?还是我需要先将此十六进制字节字符串转换为 int,然后再打印它们?
我尝试了“%x”、“%X”,但它们不起作用。
编辑:
我想补充一点,我正在制作一个 .com 可执行文件,所以我需要将它们打印为十六进制字节。
【问题讨论】:
-
你是如何给 bytes[] 数组赋值的? “01”是字符串?
-
它们被分配了字符串。所以在 ascii 中:“48 49”
-
你能把实际分配给
WRITEME[counter].byte1吗?错误的方式很多,正确的方式更少。 -
这里是一个例子:char * returner; strncpy(WRITEME[index].byte1, "00", 2); strncpy(WRITEME[index].byte2, returner, 2);
-
考虑一个数组:
char byte[9][2];,而不是 9 个结构成员。记住数组索引从0开始。
标签: c formatting printf