0
点赞
收藏
分享

微信扫一扫

数字和字符串之间的转换


1.数字到字符串

// charint.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[])
{
int num = 12345;
int j = 0, i = 0;

printf("intnum = %d\n", num);

char temp[7], str[7];

while (num)
{
temp[i] = num%10 + '0';
i++;
num = num/10;
}
temp[i] = 0;
i = i - 1;
//std::cout << "temp = " << temp << std::endl;
printf("strtemp = %s\n", temp);

// 反转字符串
while(i >= 0)
{
str[j] = temp[i];
j++;
i--;
}
str[j] = 0;

//std::cout << "String = " << str << std::endl;
printf("string = %s\n", str);

return 0;
}

数字和字符串之间的转换_printf


2.字符串到数字

// charint.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int num = 12345;
int j = 0, i = 0, sum = 0;
char temp[7] = {'1', '2', '3', '4', '5', '\0'}, str[7];
printf("strtemp = %s\n", temp);

while (temp[i])
{
sum = sum*10 + (temp[i] - '0');
i++;
}
printf("intsum = %d\n", sum);

return 0;
}


数字和字符串之间的转换_iostream_02



举报

相关推荐

字符串与数字转换

0 条评论