显示小数,八进制和十六进制

时间:2018-10-05 22:15:37

标签: c hex printf decimal octal

我试图显示以下菜单,但是当我输入一个大于3位的值时,空格将关闭。有什么建议么?

Accumulator:
     Hex    :   0000
     Octal  :   00000000
     Decimal:   0

    void print_acc(short acc){
        printf("\n");
        printf("****************************************\n");
        printf("* Accumulator:                         *\n");
        printf("*   Hex     :   %04hx                   *\n", acc);
        printf("*   Octal   :   %08ho               *\n", acc);
        printf("*   Decimal :   %hd                    *\n", acc);
        printf("****************************************\n");

    }

2 个答案:

答案 0 :(得分:0)

您的十进制打印f上没有任何宽度定义。否则,这将数字对齐就好了。请记住,如果您的数字大于指定的宽度,则将调整间距以适应较长的宽度。

****************************************
* Accumulator:                         *
*   Hex     :   2ac0                   *
*   Octal   :   00025300               *
*   Decimal :   10944                    *
****************************************

如果您将宽度说明符添加到十进制并删除0填充,如下所示:

void print_acc(short acc){
    printf("\n");
    printf("****************************************\n");
    printf("* Accumulator:                         *\n");
    printf("*   Hex     :   %8hx               *\n", acc);
    printf("*   Octal   :   %8ho               *\n", acc);
    printf("*   Decimal :   %8hd               *\n", acc);
    printf("****************************************\n");
}

您将使一切对齐:

****************************************
* Accumulator:                         *
*   Hex     :       2c07               *
*   Octal   :      26007               *
*   Decimal :      11271               *
****************************************

如果要左对齐数字,请在宽度说明符之前添加--(例如:%-8hd)。

答案 1 :(得分:0)

您可以索引到一串空格以更改行尾的间距。通过使用对snprintf()的调用返回的数字,snprintf()函数可用于直接确定每个数字的显示字符数。

这是一个示例程序,该程序在每个基数中打印出一个数字列表。 snprintf()返回如果足够大则将写入输出数组的字符数;这里的数组temp[]可以容纳64个字符或最多63位数字(因为snprint()总是写一个空终止符)。该数量应该足够大以避免出现问题,但是即使该数字太大而无法放入temp[]数组中,snprintf()也不会导致缓冲区溢出,并且返回的数字仍然是正确的字符数。

一旦确定了每种显示格式的字符数,该数字将用于索引空格字符串,以便所有行尾对齐。请注意其中包含的assert()语句,以确保所显示的最大数字不会太大,以至于指针算术偏离了spaces[]数组的末尾。

#include <stdio.h>
#include <assert.h>

#define TEMP_SZ  64

int main(void)
{
    enum bases { OCTAL, DECIMAL, HEX };

    short arr[] = { 0, 16, 256, 512, 1024, 16384 };
    size_t arr_sz = sizeof arr / sizeof *arr;

    for (size_t i = 0; i < arr_sz; i++) {
        int num_sz[3];
        int num = arr[i];
        char temp[TEMP_SZ];

        /* Find width of number display */
        num_sz[DECIMAL] = snprintf(temp, TEMP_SZ,"%hd", num);
        num_sz[OCTAL] = snprintf(temp, TEMP_SZ, "%08ho", num);
        num_sz[HEX] = snprintf(temp, TEMP_SZ, "%04hx", num);

        /* Strings to precede termination of display lines */
        char stars[] = "*********************";
        char spaces[] = "                     ";

        /* Avoid undefined behavior */
        assert((size_t) num_sz[OCTAL] < (sizeof spaces / sizeof *spaces));

        puts("");
        printf("******************%s*\n", stars);
        printf("* Accumulator:    %s*\n", spaces);
        printf("*   Hex      :    %04hx%s*\n", num, spaces + num_sz[HEX]);
        printf("*   Octal    :    %08ho%s*\n", num, spaces + num_sz[OCTAL]);
        printf("*   Decimal  :    %hd%s*\n", num, spaces + num_sz[DECIMAL]);
        printf("******************%s*\n", stars);
    }

    return 0;
}

程序输出:

****************************************
* Accumulator:                         *
*   Hex      :    0000                 *
*   Octal    :    00000000             *
*   Decimal  :    0                    *
****************************************

****************************************
* Accumulator:                         *
*   Hex      :    0010                 *
*   Octal    :    00000020             *
*   Decimal  :    16                   *
****************************************

****************************************
* Accumulator:                         *
*   Hex      :    0100                 *
*   Octal    :    00000400             *
*   Decimal  :    256                  *
****************************************

****************************************
* Accumulator:                         *
*   Hex      :    0200                 *
*   Octal    :    00001000             *
*   Decimal  :    512                  *
****************************************

****************************************
* Accumulator:                         *
*   Hex      :    0400                 *
*   Octal    :    00002000             *
*   Decimal  :    1024                 *
****************************************

****************************************
* Accumulator:                         *
*   Hex      :    4000                 *
*   Octal    :    00040000             *
*   Decimal  :    16384                *
****************************************