
C语言学习笔记三——将int类型的数据转换为字符串
玄之又玄,众妙之门
#include <stdio.h> #include <stdlib.h> /* * 函数: Int_To_Str(int x,char *Str) * 功能: 将整型转为字符串 * 参数: x :转换的整数 * str:转换后的字符串 */ void Int_To_Str(int x,char *Str); //函数声明 //主函数 int main() { int i =123; int j=0; char Msg[20]="ABCDEFGHIJKLMNOPQRST"; char *P_Str; Int_To_Str(i,Msg); P_Str = &Msg; while(*P_Str!='\\0') { printf("%c",*P_Str); P_Str++; } printf("\\n"); system("pause"); return 0; } //函数定义 void Int_To_Str(int x,char *Str) { int t; char *Ptr,Buf[5]; int i = 0; Ptr = Str; if(x < 10) // 当整数小于10,转换为0x格式 { *Ptr ++ = '0'; *Ptr ++ = x+0x30; } else { while(x > 0) { t = x % 10; x = x / 10; Buf[i++] = t+0x30; // 通过计算把数字编成ASCII码形式 } i -- ; for(;i >= 0;i --) // 将得到的字符串倒序 { *(Ptr++) = Buf[i]; } } *Ptr = '\\0'; }👁️ 阅读量:0
© 版权声明:本文《C语言学习笔记三——将int类型的数据转换为字符串》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1686626092a309150.html。