PDA

View Full Version : Char* to double, int, and back


wyrmmage
2007.06.22, 04:19 PM
A couple of months ago, I was googling around looking for a way to turn an array of chars into either a double or an int. I looked through two pages of google search results (most were forums), but couldn't find anything except for 'you have to make your own function'. This seemed a bit odd to me, since I figured that it must be a fairly common problem. Anyway, I went ahead and built my own functions for it:

int makeInt(char* theString)
{
int theNumber = 0;
int theLength = strlen(theString)-1;

bool isNegative = 0;
if(theString[0]=='-')
{
isNegative = 1;
for(int i=0; i<theLength; i++)
{
theString[i] = theString[i+1];
}
theLength = theLength-1;
}

int tempNum = 10;
for(int x=0; x<=theLength; x++)
{
for(int i=0; i<=9; i++)
{
if(theString[x]==theNumbers[i])
{
for(int z=0; z<=theLength-x-1; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber + (i * (tempNum/10));
tempNum = 10;
}
}
}

if(isNegative)
{
theNumber = theNumber * -1;
}


return theNumber;
}


and


double makeDouble(char* theString)
{
double theNumber = 0.0;
int theLength = strlen(theString)-1;
int dotPos = -1;

bool isNegative = 0;
if(theString[0]=='-')
{
isNegative = 1;
for(int i=0; i<theLength; i++)
{
theString[i] = theString[i+1];
}
theLength = theLength-1;
}

for(int i=0; i<=theLength; i++)
{
if(theString[i]=='.')
{
dotPos = i;
}
else
{
if(dotPos != -1)
{
theString[i-1] = theString[i];
if(i==theLength)
{
theString[i]='\0';
theLength = theLength - 1;
}
}
}
}

int tempNum = 10;
for(int x=0; x<=theLength; x++)
{
for(int i=0; i<=9; i++)
{
if(theString[x]==theNumbers[i])
{
for(int z=0; z<=theLength-x-1; z++)
{
tempNum = tempNum * 10;
}
theNumber = theNumber + (i * (tempNum/10));
tempNum = 10;
}
}
}

if(dotPos != -1)
{
int tempNum = 10;

for(int z=1; z<=dotPos; z++)
{
tempNum = tempNum * 10;
}

theNumber = theNumber / (tempNum/10);
}


if(isNegative)
{
theNumber = theNumber * -1;
}


return theNumber;
}


These functions worked fine, but I was always a little uncomfortable with them, figuring that they were slow and what not. Last week, I ran across these functions:
int atoi( const char *str );
double atof( const char *str );
int sprintf( char *buffer, const char *format, ... );

Why does no one recommend these? Is there something wrong with them...are they not very accurate or something, or is it just because they're C, so some C++ people think that they're 'wrong'? :\
Thanks guys :)
-wyrmmage

AnotherJake
2007.06.22, 05:27 PM
... Last week, I ran across these functions:
int atoi( const char *str );
double atof( const char *str );
int sprintf( char *buffer, const char *format, ... );

Why does no one recommend these? Is there something wrong with them...are they not very accurate or something, or is it just because they're C, so some C++ people think that they're 'wrong'? :\
Thanks guys :)
-wyrmmage

You've seen someone recommend against using them? :???:

I've never had any problems with them myself. I use sprintf A LOT! Surely you must've misread something about it.

BTW, I have a really hard time reading code that doesn't use indentation.

akb825
2007.06.22, 05:39 PM
For converting strings into other primitives, you can also use sscanf. But yes, those are very useful functions. I don't know why you couldn't find anything about them. (and they are perfectly accurate, but they may be considered somewhat "low level")

OneSadCookie
2007.06.22, 06:30 PM
sprintf can be dangerous (but snprintf or asprintf fixes that). The major issue with atoi and atof is that they don't have any way to signal that they couldn't parse the number.

There are certainly situations where each of them is entirely appropriate, though.

unknown
2007.06.22, 06:47 PM
If you are in C++ why don't you use a string stream?
http://www.cppreference.com/cppsstream/constructors.html

OneSadCookie
2007.06.22, 06:53 PM
Because it's a horrible, vile, abomination?

unknown
2007.06.22, 07:41 PM
Because it's a horrible, vile, abomination?

which is why I said "If you are in C++" :p

wyrmmage
2007.06.22, 07:48 PM
I've found stringstreams to be slow, and downright annoying (too much code to justify using them instead of my own little functions); besides, I prefer using C code as much as possible just because it seems to annoy people :lol:
Anyway, thanks for all of the quick replies everyone :)
I also had another question, and I'll try to not waste space by making another thread, since it's not a terribly important one.
I've always wondered how the string class in C++ worked...does anyone know where I could get a hold of the code for the class? Unfortunately, it's the kind of thing that I have a hard time finding on google :D
Thanks again everyone :)
-wyrmmage

OneSadCookie
2007.06.22, 09:42 PM
The source is on your machine:

/usr/include/c++/4.0.0/bits/basic_string.h

You don't want to read it!

AnotherJake
2007.06.22, 10:00 PM
You don't want to read it!
:blink:
...
I looked at it. I am nearly blind now.
...
What exactly do you mean by `want' to read it? I would say it's more like you `can't' read it. omg... [shakes head]

wyrmmage
2007.06.23, 01:26 AM
lol...needless to say, I didn't find what I wanted in there XD
What I was basically wondering was:
If I have a dynamic arrray of non-objects (or objects, I suppose), and I want to increase the size of it, is there any way to do so without making a new array, copying everything over, deleting the old array, remaking it with more elements, and then copying the copied elements back over to the original one? This doesn't seem very efficient to me, and I was hoping that vectors or strings did it another way, but I can't seem to find where that code is :D
I'm avoiding using vectors for a number of reasons...is there any way to do this? (even if it means using C functions..I don't mind)
Thanks :)
-wyrmmage

OneSadCookie
2007.06.23, 02:07 AM
if it's an array of primitive types (eg. int, float) then you can use malloc to allocate the array, free to free it, and realloc to alter its size. If it's not an array of primitives, things like copy constructors and assignment operators make that impossible.

realloc *may* be more efficient than malloc; memcpy; free, or may do just that.

wyrmmage
2007.06.23, 02:47 AM
so, can I mix and match malloc, realloc, and free with new and delete?
Say I have this:

int* theInts = new int[40];
for(int i=0; i<=39; i++)
{
theInts[i] = 0;
}
theInts = (int*) realloc(theInts, 80 * sizeof(int));
for(int i=40; i<=79; i++)
{
theInts[i] = 0;
}
//now I have 79 elements with 0 in them, right?
delete [] theInts;


Is that legal? or am I going to have to use malloc if I use realloc?
Thanks man :)
-wyrmmage

JeroMiya
2007.06.23, 05:12 PM
so, can I mix and match malloc, realloc, and free with new and delete?

...

Is that legal? or am I going to have to use malloc if I use realloc?
Thanks man :)
-wyrmmage

It's best to only delete things that are new'ed, and free only things that were malloc'ed. You might be able to do it but it's not guaranteed to work as far as I know.

bronxbomber92
2007.06.23, 05:36 PM
new/delete calls the objects constructor/deconstructor. So using it on a malloced if you're working with pure data type, is *should* work as far as I know.

OneSadCookie
2007.06.23, 09:00 PM
It is *not* safe to delete something you've malloc()ed, nor to free() something you've new'd. Don't do it.

Also, remember to delete[] stuff you new[]!

Hog
2007.06.24, 08:42 AM
the stl vector is not that inefficient in reallocation as tries to work around the problem by allocating extra space for the vector.
if that's still too inefficient for you you could go with a list which however brings up issues concerning accessibility, but that pretty much depends on your requirements.

wyrmmage
2007.06.24, 03:03 PM
wait...did I use delete [] wrong in my example code? I thought that code would delete all of the memory new [] 'ed?
Thanks for the advice everyone :)
-wyrmmage

akb825
2007.06.24, 03:09 PM
If you didn't use realloc it would be correct. Of course, using realloc with new[] (or even just new, along with using free with either of the news) isn't guaranteed to work.