gpt4 book ai didi

c - 显示 8 位十六进制偏移量 0x?

转载 作者:行者123 更新时间:2023-11-30 18:29:37 25 4
gpt4 key购买 nike

到目前为止,我只致力于第一个显示十六进制偏移量“0”的工作,我正在尝试找到一种方法让程序打开文件并显示带有前导 0x 指示符的 8 位十六进制偏移量。我试图让它运行这样的事情:

----------------------------------------------------------------------------- 
offset 0 1 2 3 4 5 6 7 8 9 a b c d e f ascii
-----------------------------------------------------------------------------
0x00000000
0x00000010
0x00000020

等等...

任何提示/建议都会很棒。另外,我也在寻找一种“更干净”的方式来显示横幅,而不必 printf 单独的行等等。

所以,我的问题是:

  1. 如何让程序显示以 0x 开头的十六进制偏移量。我尝试插入该部分的代码,但我无法完全确定如何插入,因此任何提示将不胜感激。
  2. 有没有更简洁的方式来显示标题?

非常感谢。

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>


int main(int argc, char** argv)
{
struct winsize terminal;
FILE* fp;
int a, c, column, row, lines;

ioctl(0, TIOCGWINSZ, &terminal, argv[2]);

fp = fopen(argv[1], "r"); //set open for argv[1] and set it to read mode
column = terminal.ws_col;
row = terminal.ws_row;
lines = atoi(argv[2]);

// printf("Rows are: %d\n", row);
// printf("Columns are: %d\n", column);

if (argc < 3) //Argument checking, at least three to run
{
printf("You need atleast 3 arguments to run!\n");
exit(1);
}

if (fp == NULL)
{
fprintf(stderr, "Error opening '%s'.\n", argv[1]); //if not file is specified then close program and give an error
exit(1);
}
c = fgetc(fp); //set c to fgetc

if (column < 80 || row < 20) //if temrinal window is less than 80x20, display an error and exit
{
printf("Terminal window must be at least 80x20!\n"); //display error and close program if column criteria isn't met
exit(1);
}

/*
while(!feof(fp)) //if file is entered, display a test message (this is where later on I will show hex value)

{
printf("Good Job! You picked a file to manipulate!!\n");
exit(1);
}
*/
if (lines == 0) //if 0 is entered for argv[2], display all the lines, 512 byte file
{
printf("------------------------------------------------------------------------------\n"); //**this is where I try to display character/hex banner**
printf("offset 0 1 2 3 4 5 6 7 8 9 a b c d e f ascii\n");
printf("-------------------------------------------------------------------------------\n");
for (c == 0x200) ; //**This is where I try to get the 8 digit 0x hex offset**
{
fprintf(stdout, "%04x", c);
}
}
else if (lines == 20)
{
printf("This will print out 20 lines\n");
}
else if (lines == 30)
{
printf("This will print out 30 lines\n");
}
else
{
printf("Please Enter a Valid Number!\n");
}

fclose(fp); //close fp

return 0;
}

最佳答案

尝试

printf("0x%08x\n", 0xDEADBEEF);

关于c - 显示 8 位十六进制偏移量 0x?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36360456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com