Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

cowsarenotevil

Converting a char to an int

This topic is 5561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I ever get my file loader to work, is there a way to convert a char to an int? Atoi is too slow for me, and unsupported on a lot of compilers... Thanks -~-The Cow of Darkness-~-

Share this post


Link to post
Share on other sites
Advertisement
atoi() is a pretty standard function. I'd be interested in knowing what compilers this fails on. Unless, your file is really, really big atoi should do the job. If is really is too slow perhaps it may be better to store your data in a binary format.

---------
Andrew



[edited by - acraig on April 1, 2003 12:24:20 AM]

Share this post


Link to post
Share on other sites
do you mean char* -> int or char -> int?

if you don''t like itoa then try out sprintf() not sure how fast it is, but it''s easy to use. if you want to just go char->int you can always just go int i = (int)ch;

Share this post


Link to post
Share on other sites
I meant char*, and yes, my file is huge, it''s a 10000 polygon wavefront obj file, and sprintf is only for int to char, according to the msdn.

-~-The Cow of Darkness-~-

Share this post


Link to post
Share on other sites

    
#include <sstream>

int main() {
int number;
std::stringstream ss;
ss << "2562";
ss >> number;
}
It's supported by all C++ standard compilant compilers, but probably even slower than atoi. Are you sure atoi is your bottleneck? Converting, say, 30000 numbers from string to int shouldn't take more than a few milliseconds..

[edited by - civguy on April 2, 2003 2:28:55 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Ecko
do you mean char* -> int or char -> int?

if you don''t like itoa then try out sprintf() not sure how fast it is, but it''s easy to use. if you want to just go char->int you can always just go int i = (int)ch;


For reference, char is automatically casted up to an int so your cast in unnecessary.



Qui fut tout, et qui ne fut rien
Invader''s Realm

Share this post


Link to post
Share on other sites
Yeah, I remember reading somewhere that atoi is sometimes not available.
Try strtol - it''s minimally slower (variable base), and better be there

sprintf is slow. Also, your reading of MSDN is incorrect.

quote:
Are you sure atoi is your bottleneck? Converting, say, 30000 numbers from string to int shouldn''t take more than a few milliseconds..

Yep. I don''t think there are significant gains to be had over atoi / strtol, either (the algorithm can''t be parallelized effectively).
Are you using the DLL CRT? An inlined version would of course be quite a bit faster.

Share this post


Link to post
Share on other sites
char buffer[128];
long num1=1;

sprintf( buffer, "%lu", num1 );

- - -
long buffer=0;
char num[2]=23;

sscanf( num, "%lu", &buffer ); // notice the & before buffer

- - -


This is how you use sprintf(),sscanf().

.lick


[edited by - Pipo DeClown on April 2, 2003 1:30:06 PM]

Share this post


Link to post
Share on other sites
I think sscanf looks like it would work, and I''ve had a lot of people tell me that atof wouldn''t be slow, but to convert as many numbers as I need to it''s very slow. I have tested it many times.

-~-The Cow of Darkness-~-

Share this post


Link to post
Share on other sites
Try this:

  
double myatof(const char* c) {
double num = 0;
double sgn = 1;
if (*c == ''-'') { sgn = -1.0; ++c; }
if (*c == ''+'') { ++c; }
while (*c != ''.'') {
num *= 10;
num += *c - ''0'';
++c;
}
++c;
double div = 0.1;
while (*c != ''\0'') {
num += (double)(*c - ''0'') * div;
div *= 0.1;
++c;
}
return num * sgn;
}

It isn''t very safe. The number must have a ''.'' in it, and it can''t have any extra characters. But since on my test on VS.NET it was over 20 times faster than atof, I must''ve checked it wrong or then atof is *really* slow.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!