C語言將十進位制整數輸出為八進位制和十六進位制
阿新 • • 發佈:2019-01-03
方法一:
直接使用控制字串
%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; }