## Char* to double, int, and back

Moderator
Posts: 370
Joined: 2006.08
Post: #1
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:
Code:
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

Code:
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

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Moderator
Posts: 3,567
Joined: 2003.06
Post: #2
wyrmmage Wrote:... 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.
Moderator
Posts: 1,140
Joined: 2005.07
Post: #3
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")
Luminary
Posts: 5,139
Joined: 2002.04
Post: #4
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.
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
If you are in C++ why don't you use a string stream?
http://www.cppreference.com/cppsstream/c...ctors.html

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Luminary
Posts: 5,139
Joined: 2002.04
Post: #6
Because it's a horrible, vile, abomination?
Sage
Posts: 1,403
Joined: 2005.07
Post: #7

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

Sir, e^iÏ€ + 1 = 0, hence God exists; reply!
Moderator
Posts: 370
Joined: 2006.08
Post: #8
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
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
Thanks again everyone
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Luminary
Posts: 5,139
Joined: 2002.04
Post: #9
The source is on your machine:

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

You don't want to read it!
Moderator
Posts: 3,567
Joined: 2003.06
Post: #10

...
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]
Moderator
Posts: 370
Joined: 2006.08
Post: #11
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
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

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Luminary
Posts: 5,139
Joined: 2002.04
Post: #12
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.
Moderator
Posts: 370
Joined: 2006.08
Post: #13
so, can I mix and match malloc, realloc, and free with new and delete?
Say I have this:
Code:
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

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Member
Posts: 45
Joined: 2006.11
Post: #14
wyrmmage Wrote: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.
Member
Posts: 312
Joined: 2006.10
Post: #15
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.