Solved

String to int's

Posted on 2004-09-07
13
1,371 Views
Last Modified: 2008-02-01
Hello,

Could anyone advice if there is a function in C++ to convert a string to small ints, 0-255.

And a string to float's

Thanks
0
Comment
Question by:AntoniRyszard
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 13

Expert Comment

by:SteH
Comment Utility
The C++ way would be using stringstreams. Find more ways at:
http://www.codeguru.com/forum/showthread.php?s=&threadid=231054
0
 

Author Comment

by:AntoniRyszard
Comment Utility
Something like this?

   string strInt = "125";
   string strFloat = "125.5";

   int i = atoi(strInt);
   float f = atof(strFloat);

And should the string work, as a  char * does?
0
 
LVL 13

Accepted Solution

by:
SteH earned 20 total points
Comment Utility
no for STL string's you need to use

int i = atoi (strInt.c_str ());
float f = atof (strFloat.c_str ());
0
 

Author Comment

by:AntoniRyszard
Comment Utility
Thanks
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
No, string is not the same as char *. You can get a const char * at the contents of the string with the .c_str() method:

   string strInt = "125";
   string strFloat = "125.5";

   int i = atoi(strInt.c_str());
   float f = atof(strFloat.c_str());

As SteH says, you can also use strstream to do the work without having to use C-style strings.

-bcl
0
 

Author Comment

by:AntoniRyszard
Comment Utility

Are there any advantages of using char * as apposed to strings?

I thought strings's were just a modern addition to the language, but much the same as a char *.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 13

Expert Comment

by:SteH
Comment Utility
strings have memory management built in. You can add to a string chars, char* and strings without the necessity to look whether the allocated memory is sufficient. So I would see the advantages on the strings side.
0
 

Author Comment

by:AntoniRyszard
Comment Utility
Hello,

I just remembered the variables I wanted to convert to an int, were char[]

I used this code to get the values from a file:

sscanf(buffer, "%s %s %s", &field0, &field1, &field2);

but as I understand although field0,1,2 were delcared as 200 char[] in size. If I then store the field0 value as a string:

string temp = field0.

The empty spaces from field0 would not be stored in temp.

So could I still use the atoi, I tried strlwr(field0); and this worked.

Thanks
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
Yes, string.c_str () will give you a const char* to be used as any other char* (or char[]).

But in C++ the way to go should be
string buffer;
stringstream ss (buffer); //generate a string stream from the buffer.
int f0, f1, f2;
ss >> f0 >> f1 >> f2; // reading is done with type checking unlike sscanf

or for strings you can do:
string sf0, sf1. sf2;
ss >> sf0 >> sf1 >> sf2;
as if ss was a normal stream (like a file).
0
 

Author Comment

by:AntoniRyszard
Comment Utility
The complete code is, I think it works: Does everything seem ok to you?

Each line in the file, has three strings

struct has three variables:

string field0;
string field1;
int field2;

void function(char * fileName){
   char buffer[300];
   char field0[100], field1[100], field2[100];  
   ifstream in(fileName);  
   
   while(!in.eof()){  
      in.getline(buffer, 300);          
      int stringCount = sscanf(buffer, "%s %s %s", &field0, &field1, &field2);
      programStrut newObject;
     
      if(stringCount == 3){        
         
         newObject.field0 = strlwr(field0);
         newObject.field1 = atoi(field1);
         newObject.field2 = strlwr(field2);      
     
         programVector.push_back(newObject);
      }  
}      
0
 
LVL 11

Expert Comment

by:vo1d
Comment Utility
try using :
std::string strInt = "125";
int i = atoi(&strInt[0]);

&strint[0] gives you a char pointer instead of a const char pointer.
0
 
LVL 11

Expert Comment

by:vo1d
Comment Utility
no, cause field2 is declared as int in your struct but you try to pass a string to it.
i think it has to be:

 newObject.field0 = strlwr(&field0[0]);
 newObject.field1 = strlwr(&field1[0]);
 newObject.field2 = atoi(&field2[0]);
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
>> sscanf(buffer, "%s %s %s", &field0, &field1, &field2);
>> string temp = field0.
>> The empty spaces from field0 would not be stored in temp.
I think you make an error here. field0 won't contain any spaces since sscanf will use any whitespace in buffer to terminate a string from the next one. Whereas the string temp will contain everything of field0 up to its terminating 0.

And for me the construct to scan first strings from a buffer string to convert them later to numbers seem overcomplication. Why don't you use
sscanf (buffer, "%s%s%d", &newObject.field0, &newObject.field1, &newObject.field2); // format string could be equally "%s %s %d"
No need to convert field 2 from char to int later.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now