Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
for(int i=0; i<128; i++)
{
string str = "";
char chr = i;
stringstream chartostr;
chartostr << chr;
chartostr >> str;
}
i want to convert char to string, above code is work corretly, but is there a pro way to do this ?
Posted

I would replace
Quote:
string str = "";
char chr = i;
stringstream chartostr;
chartostr << chr;
chartostr >> str;
with

C++
string str;
str += static_cast<char>(i);
</char>


I don't understand the purpose of the for loop. What are you trying to do?
 
Share this answer
 
With str declared inside the body of your for loop, when the loop exits you have nothing to show for it! Wouldn't something like this get you what you want?
C++
string Ascii7(128, '\0');
for (int i = 1; i < 128; ++i)
{
  Ascii7[i] = static_cast<char>(i);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900