Click to See Complete Forum and Search --> : Easy one
Boumxyz2
April 3rd, 2002, 01:20 PM
Just need to know where I can find source code for stdlib.h... or Atoi function. I need to do a Alphanumeric to Octal and I don't want to recode everything since ATOI doing same thing except that instead of using base 10 I'll use base 8.
So I'm pretty sure you can find the source code out there just don't know where..
Nicolas Bohemier
______________
Un sourire ne coûte rien, mais il rapporte beaucoup; il enrichit celui qui le reçoit sans appauvrir celui qui le donne.
Frank Irving Fletcher
Boumxyz2
April 3rd, 2002, 01:57 PM
Nevermind I recoded it. here it is
#include <math.h>
#include <string.h>
#include "base.h"
int AtoO (char * string,unsigned long &Retour)
{
char power = 0;
Retour = 0;
if (strlen(string)<=0)
{
return -1;
}
for (int i = strlen(string) -1; i>=0; i--)
{
if (string[i] != '8' && string[i] != '9')
{
Retour = Retour + ((string[i] - '0') * pow(8,power) );
power ++;
}
else
{
Retour = 0;
return -2;
}
}
return 0;
}
Don't know why doesn't want to show braces
Nicolas Bohemier
______________
Un sourire ne coûte rien, mais il rapporte beaucoup; il enrichit celui qui le reçoit sans appauvrir celui qui le donne.
Frank Irving Fletcher
Alexis Moshinsky
April 3rd, 2002, 02:34 PM
Hi Nicolas,
using of atoi is unsafe. F.e. it will return 43 in follow situation:
int result = atoi("43!21");
CRT library has better set of functions -strto...
There is one of them:
_CRTIMP long __cdecl strtol(const char*, char**, int);
The second parameter is address of char* - will point to bad array entry or will contain array + strlen(array) address if no error occure.
Third parameter is base.
The function's source is copied from SDK sources (file strtol.c)
long __cdecl strtol (
const char *nptr,
char **endptr,
int ibase
)
{
return (long) strtoxl(nptr, endptr, ibase, 0);
}
static unsigned long __cdecl strtoxl (
const char *nptr,
const char **endptr,
int ibase,
int flags
)
{
const char *p;
char c;
unsigned long number;
unsigned digval;
unsigned long maxval;
p = nptr; /* p is our scanning pointer */
number = 0; /* start with zero */
c = *p++; /* read char */
while ( isspace((int)(unsigned char)c) )
c = *p++; /* skip whitespace */
if (c == '-') {
flags |= FL_NEG; /* remember minus sign */
c = *p++;
}
else if (c == '+')
c = *p++; /* skip sign */
if (ibase < 0 || ibase == 1 || ibase > 36) {
/* bad base! */
if (endptr)
/* store beginning of string in endptr */
*endptr = nptr;
return 0L; /* return 0 */
}
else if (ibase == 0) {
/* determine base free-lance, based on first two chars of
string */
if (c != '0')
ibase = 10;
else if (*p == 'x' || *p == 'X')
ibase = 16;
else
ibase = 8;
}
if (ibase == 16) {
/* we might have 0x in front of number; remove if there */
if (c == '0' && (*p == 'x' || *p == 'X')) {
++p;
c = *p++; /* advance past prefix */
}
}
/* if our number exceeds this, we will overflow on multiply */
maxval = ULONG_MAX / ibase;
for (;;) { /* exit in middle of loop */
/* convert c to value */
if ( isdigit((int)(unsigned char)c) )
digval = c - '0';
else if ( isalpha((int)(unsigned char)c) )
digval = toupper(c) - 'A' + 10;
else
break;
if (digval >= (unsigned)ibase)
break; /* exit loop if bad digit found */
/* record the fact we have read one digit */
flags |= FL_READDIGIT;
/* we now need to compute number = number * base + digval,
but we need to know if overflow occured. This requires
a tricky pre-check. */
if (number < maxval || (number == maxval &&
(unsigned long)digval <= ULONG_MAX % ibase)) {
/* we won't overflow, go ahead and multiply */
number = number * ibase + digval;
}
else {
/* we would have overflowed -- set the overflow flag */
flags |= FL_OVERFLOW;
}
c = *p++; /* read next digit */
}
--p; /* point to place that stopped scan */
if (!(flags & FL_READDIGIT)) {
/* no number there; return 0 and point to beginning of
string */
if (endptr)
/* store beginning of string in endptr later on */
p = nptr;
number = 0L; /* return 0 */
}
else if ( (flags & FL_OVERFLOW) ||
( !(flags & FL_UNSIGNED) &&
( ( (flags & FL_NEG) && (number > -LONG_MIN) ) ||
( !(flags & FL_NEG) && (number > LONG_MAX) ) ) ) )
{
/* overflow or signed overflow occurred */
errno = ERANGE;
if ( flags & FL_UNSIGNED )
number = ULONG_MAX;
else if ( flags & FL_NEG )
number = (unsigned long)(-LONG_MIN);
else
number = LONG_MAX;
}
if (endptr != NULL)
/* store pointer to char that stopped the scan */
*endptr = p;
if (flags & FL_NEG)
/* negate result if there was a neg sign */
number = (unsigned long)(-(long)number);
return number; /* done. */
}
Boumxyz2
April 3rd, 2002, 03:46 PM
Thx but I've developped mine which isn't really robust but for the task I need it I don't need it to be robust.
but anyway I'm using mine and not atoi.. I'm pretty screwed so far..
It seems I get an overflow on a long while sizeof long are 4 on my computer.. which means 32 bits... but a number higher than 16 doesn't computer it really sucks..
Nicolas Bohemier
______________
Un sourire ne coûte rien, mais il rapporte beaucoup; il enrichit celui qui le reçoit sans appauvrir celui qui le donne.
Frank Irving Fletcher
Paul McKenzie
April 3rd, 2002, 05:15 PM
Don't use the variable "i" in closed brackets. That is why the post is coming out in italics. Instead, do this "[ i ]" (note the spaces between the "i" and the brackets.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.