整数を16進数列へ変換する


関数名
int2hexsz  整数を16進数列へ変換する
形式
void int2hexsz(char *str, int num);
引数
str  (出力)16進数ASCIZ文字列
num  (入力)変換したい整数
関数値
なし
注意事項

用例(int2hexsz-test.c
char str[15];
int2hexsz(str, 12345)

プログラム(int2hexsz.c
void int2hexsz(char *str, int num)
{
    int i;
    static void rint2hexsz();

    i = 0;
    if (num < 0) {
        num = -num;
        *str = '-';
        i++;
    }
    rint2hexsz(str, num, &i);
    *(str + i) = '\0';
}

static void rint2hexsz(char *str, int num, int *off)
{
    int k, n;

    if ((k = num >> 4) != 0) rint2hexsz(str, k, off);
    n = num & 0xf;
    *(str + *off) = n <= 9 ? n + '0' : n - 10 + 'A';
    (*off)++;
}
説明
再帰法を使用。先行の数字については自分自身を呼出して対応し、 最終桁だけを変換して処理する。その際、A-F への変換に注意す ること。

関連関数
整数を2進数列へ変換する整数を10進数列へ変換する2進数列を整数へ変換する10進数列を整数へ変換する16進数列を整数へ変換する