A summary of the transition between CString, char *, char [], string, int, double

Note: The fp variable in the below is defined by myself. You can ignore it.
Please also take your attention:
1. The CString is a wide string(word not byte), which is a part of MFC. The others are narrow string.
2. They are run in visual studio.

1. char * to CString

method 1: Use Format


char *temp = "Hello MFC";

CString str;

str.Format(L"%S\n", temp);
//Notice that S is uppercase, otherwise it will be garbled

MessageBox(str);

Method 2: Use the constructor directly


char *temp1 = "你好";

CString str1(temp1); 
//This way will not be garbled code, use method one, 
//Chinese will be garbled code

MessageBox(str1);

2. CString to char *


CString str("Hello MFC");
CString str1("你好");

//This must be in the closest position before using T2A
USES_CONVERSION;
char *buf = T2A(str);
char *buf1 = T2A(str1);

3. char [] to CString

Method 1: Use Format


char temp[10] = "Hello MFC";

CString str;
str.Format(L"%S\n", temp);
//Notice that S is uppercase, otherwise it will be garbled
MessageBox(str);

Method 2: Use the constructor directly


char temp1[10] = "你好";
CString str1(temp1); 
//This way will not be garbled code, use method one, 
//Chinese will be garbled code

MessageBox(str1);

4. CString to char[]

//First convert CString to char *
CString str1("你好");

//This must be in the closest position before using T2A
USES_CONVERSION;
char *buf = T2A(str);

//Use strcpy convert char* to char[]
char temp[10];
strcpy(temp, buf1);
fprintf(fp, "%s\n", temp);

5. string to char*


string s1 = "abc你好";
const char *tt = s1.c_str();
const char *t = s1.data();
fprintf(fp,"%s\t%s\n", tt, t);

6. char* to string


char *temp = "abc你好";
string str = temp;
fprintf(fp, "%s\n", str.c_str());

7. char[] to string

char temp[14] = "abc你好def";
string str = temp;
fprintf(fp, "%s\n", str.c_str());

8. string to char[]

method 1: First convert string to char *, then convert to char[]

string str = "hello world";
const char *temp = str.c_str();
char temp1[20];
strcpy(temp1, temp);
fprintf(fp, "%s\n", temp);

method 2: Convert byte by byte

string str = "hello world";
char temp2[20];
int i = 0;
for (i = 0; i < str.length(); i++) {
	temp2[i] = str[i];
}
temp2[i] = '\0';
fprintf(fp, "%s\n", temp2);

9. int to char*

Method 1: Use sprintf

int i = 123;
char str[10];
sprintf(str, "%d", i);
fprintf(fp, "%s\n", str);

Method 2: Use _itoa

 
int number = 12345;
char str[32];
_itoa(number, str, 10); 
//The third parameter means the scale. 10 means dec, 2 means binary.
fprintf(fp, "%s\n", str);

10. char * to int

char *str = "123";
int num = atoi(str);
fprintf(fp, "%d\n", num);

11. int,double to CString

int a = 123;
double b = 123.4;
CString str;
str.Format(L"%d %lf", a, b);
MessageBox(str);

12. CString to int,double

CString str("123");
int num = _ttoi(str);
fprintf(fp, "%d\n", num);

CString str1("123.4");
double num1 = _ttof(str1);
fprintf(fp, "%lf\n", num1);

13. int,double to string


int num = 123;
double number = 123.4;

string sss = to_string(num);
string str = to_string(number);
fprintf(fp, "%s, %s\n", sss.c_str(), str.c_str());

//Can't use fprintf(fp, "%s, %s\n", sss, str); 
//Because sss is the string type,it belongs to STL container. 
//but %s format means to print string. sss.c_str() is the string.

14. string to int,double

string ss = "124";
string sss = "123.4";
int num = atoi(ss.c_str());
double number = atof(sss.c_str());
fprintf(fp, "%d, %lf\n", num, number);

Have fun.

If you want to know string operation, you can visit this link[http://settleques.com/index.php/2019/06/12/the-summary-of-string-operation-in-c-c/].

Share this article to your social media
Subscribe
Notify of
guest
168 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
trackback
Best software
3 years ago

Best software

[…]one of our guests recently proposed the following website[…]

trackback
Guns for Sale
3 years ago

Guns for Sale

[…]one of our guests not too long ago suggested the following website[…]

trackback
GOLDEN RETRIEVER PUPPIES FOR SALE
3 years ago

GOLDEN RETRIEVER PUPPIES FOR SALE

[…]just beneath, are various entirely not associated web pages to ours, however, they are certainly worth going over[…]

trackback
PARROTS FOR SALE
3 years ago

PARROTS FOR SALE

[…]Wonderful story, reckoned we could combine several unrelated data, nonetheless actually worth taking a look, whoa did a single learn about Mid East has got extra problerms at the same time […]

trackback
Pinball for sale
3 years ago

Pinball for sale

[…]one of our visitors not long ago suggested the following website[…]

trackback
alligator meat online
3 years ago

alligator meat online

[…]the time to study or go to the material or web-sites we have linked to beneath the[…]

trackback
g spot stimulator
3 years ago

g spot stimulator

[…]Sites of interest we have a link to[…]

trackback
g spot vibe
3 years ago

g spot vibe

[…]please check out the internet sites we adhere to, such as this one, as it represents our picks in the web[…]

trackback
sex toy reviews
3 years ago

sex toy reviews

[…]very few internet sites that come about to be in depth below, from our point of view are undoubtedly nicely worth checking out[…]

trackback
prostate milking toys
3 years ago

prostate milking toys

[…]The data talked about in the report are a few of the most effective out there […]

trackback
strap on kit
3 years ago

strap on kit

[…]The details mentioned in the report are some of the top readily available […]

trackback
Weed for Sale
3 years ago

Weed for Sale

[…]one of our visitors just lately advised the following website[…]

trackback
Weave
3 years ago

Weave

[…]the time to study or take a look at the content material or web sites we have linked to beneath the[…]

trackback
best sex toys
3 years ago

best sex toys

[…]Here is a great Blog You may Find Intriguing that we Encourage You[…]

trackback
online shopping challenge
3 years ago

online shopping challenge

[…]we came across a cool web site that you simply could possibly get pleasure from. Take a search in the event you want[…]

trackback
buy kratom
3 years ago

buy kratom

[…]we prefer to honor several other web internet sites around the web, even when they aren’t linked to us, by linking to them. Under are some webpages worth checking out[…]

trackback
common myths about sex
3 years ago

common myths about sex

[…]please visit the web sites we adhere to, like this 1, as it represents our picks through the web[…]

trackback
realistic dildo
3 years ago

realistic dildo

[…]very few sites that transpire to become comprehensive beneath, from our point of view are undoubtedly effectively really worth checking out[…]

trackback
cellphone screen repairs
3 years ago

cellphone screen repairs

[…]that will be the end of this write-up. Right here you will locate some web sites that we consider you’ll value, just click the hyperlinks over[…]

trackback
slot online
3 years ago

slot online

[…]Wonderful story, reckoned we could combine some unrelated data, nevertheless genuinely really worth taking a appear, whoa did a single learn about Mid East has got far more problerms as well […]