方法一:
直接使用控制字符串
%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;
}