c语言编程-----将十进制数转成2~36任意进制的字符串

    xiaoxiao2025-05-21  90

    (一)算法作用:

    将任意十进制数转化为2~36进制,并且以字符串形式输出到屏幕。

    (二)算法核心:

    (1)字符串逆序;(2)丢位、存储;(3)化为几进制对几取余;(4)循环

    (三)算法实现:

    //将十进制数转成2~36任意进制的字符串 #include<stdio.h> #include<string.h> void StrResever(char *str) { /*char*p=NULL; while(*p!='\0') p++;*/ char tmp; int len = strlen(str); for(int i=0,j=len-1;i<=j;i++,j--) { tmp=str[i]; str[i]=str[j]; str[j]=tmp; } str[len] = '\0'; } void Myitoa(char *str,int n,int radix) { char *chars="0123456789abcdefghijklmnopqrstuvwxyz"; int i=0; while(n!=0) { str[i++]=chars[n%radix]; n/=radix; } StrResever(str); } int main() { char str[200]=" "; Myitoa(str,16,16); printf("%s\n",str); Myitoa(str,123,10); printf("%s\n",str); Myitoa(str,67,16); printf("%s\n",str); Myitoa(str,18,20); printf("%s\n",str); Myitoa(str,123,20); printf("%s\n",str); Myitoa(str,18,16); printf("%s\n",str); //char str[]=" "; //printf("%d\n",Myitoa(,10,16)); return 0; }

    (四)运行结果:

    希望对您有所帮助! 不足之处还请多多指教!

    最新回复(0)