//int转换成char*的函数 #include <iostream> #include <string.h> using namespace std; void intToS(int num, char* str) { char temp[100]; bool isNegative = false; if (num < 0) { isNegative = true; num = -num; } int i = 0; do { temp[i++] = (num % 10)+'0'; num = num / 10; } while (num); //这样能保证至少执行一次 //这样的话0执行一次后变为0 if (isNegative) { temp[i++] = '-'; } temp[i] = '\0'; //将其复制到str字符串中 int j = 0; for (int i = strlen(temp)-1; i >= 0; i--) { str[j++] = temp[i]; } str[j] = '\0'; } int main() { char s[100]; intToS(0,s); cout << s << endl; system("pause"); return 0; }
点赞 1

相关推荐

一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务