方法一:
直接使用控制字符串
%o 八進制
%x %X 十六進制
方法二:
函數 char *itoa(int value, char *string, int radix)
返回值類型char
參數value 待轉換的數字
參數string 轉換後存儲到string中
參數radix 轉換到幾進制
定義在 stdlib.h
代碼如下:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main()
{
int userinput;
printf("Please enter a integer.\n");
scanf("%d",&userinput);
char octal[MAX],hex[MAX];
itoa(userinput,octal,8);
itoa(userinput,hex,16);
printf("Octal and Hex of the integer %d that you entered is %s and %s.\n",userinput,octal,hex);
return 0;
}