int type into the char array / string Cstring TCHAR Huzhuan Assembly

Are two ways

1. int num type type into char and char * s in the array
 char s[100]="";
 char* who = "I";
 char* whom = "CSDN";
sprintf(s, "%s love %d.", who, 345);
2. // char to int variables
a=0;
char str[10]="12345";
a=atoi(str); 
3. 用 char *itoa(int value, char *string, int radix);
Given itoa (i, num, 10);
 i ---- 需要转换成字符串的数字
 num ---- 转换后保存字符串的变量1
 0 ---- 转换数字的基数(即进制)。10就是说按10进制转换数字。还可以是2,8,16等等你喜欢的进制类型
 https://zhidao.baidu.com/question/115633234.html
4. string char Huzhuan
string s1;
const char *pc = "a character array";
s1 = pc; // ok
char *str = s1; // 编译时刻类型错误
const char *str = s1.c_str(); // ok

string str="abc"; 
char *p=str.data(); 

string str="gdfd"; 
char *p=str.c_str(); 
5. Cstring string Huzhuan
		string testname = "结果:" + name;
		CString m_str(testname.c_str());

		CString namestr;
		string name = CT2A(namestr.GetBuffer()); //unicode编码,通常是这个
		string name = namestr.GetBuffer(); //非unicode编码
6. TCHAR string Huzhuan
std::string TCHAR2STRING(TCHAR* str)

{

	std::string strstr;

	try
	{

		int iLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);



		char* chRtn = new char[iLen * sizeof(char)];



		WideCharToMultiByte(CP_ACP, 0, str, -1, chRtn, iLen, NULL, NULL);



		strstr = chRtn;

	}

	catch (std::exception e)
	{

	}



	return strstr;

}

std::string m_csFileName = std::string("hello");

TCHAR wc[MAX_PATH];

#ifdef UNICODE

 _stprintf_s(wc, MAX_PATH, _T("%S"), m_csFileName.c_str());//%S宽字符

#else

 _stprintf_s(wc, MAX_PATH, _T("%s"), m_csFileName.c_str());//%s单字符

#endif

Test code

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

void main()
{

 char c[10]="abc";    // char型数组
 int num=3;    // int型数
 char s[100]="";
 char* who = "I";
 char* whom = "CSDN";

 sprintf(s, "%s love %d %s.", who, num,whom);//方法一 int型num与char*型放入char数组s中、**
 printf("拼凑后的s字符串:%s \n\n",s);


 printf("c字符串:%s \n",c);
 c[4]=c[3] = num+'0';//方法二 int型num放入char数组c中
 printf("拼凑后的c字符串:%s\n\n",c);


 int a=0;
 char str[10]="12345";
 a=atoi(str); //char转换为int变量
 printf("a=%d \n",a);


}

result

Released nine original articles · won praise 9 · views 2954

Guess you like

Origin blog.csdn.net/Rice__/article/details/90611026