编写一个函数itob(int n,char s[], int b),将整数n转换为以b进制的数。保存到s中。 #include <stdio.h> void reverse(char*left, char *right) { while(left < right) { chartmp = *left; *left = *right; *right = tmp; left++; right--; } } void itob(int n, char s[], int b) { char*start; char*end; start = s; while(n) { if (b <= 10) *s = (n %b) + ‘0‘; else if (b == 16) *s = "0123456789abcdef"[n % b]; s++; n /= b; } *s = ‘\0‘; end = s - 1; reverse(start, end); } int main() { int num= 1234; chararr[20]; int b =0; scanf("%d",&b); itob(num, arr, b); printf("%s\n",arr); system("pause"); return0; }
原文地址:http://760470897.blog.51cto.com/10696844/1705265