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

时间:2016-04-01 15:43:16

标签: c

到目前为止,我只处理第一个显示十六进制偏移量" 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. 有没有更简洁的方式来显示标题?
  3. 非常感谢你。

    #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;
    }
    

1 个答案:

答案 0 :(得分:4)

尝试

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