• C

C: __in64 equivalent of atoi() and atol()

Hello,

What is the __int64 equivalent function of atoi() and atol(). I have a utility program that outputs  an __int_64 text value, and need to have the calling program convert it back to an __int64.

Thanks
Steve
LVL 4
Stephen KairysTechnical Writer - ConsultantAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
HooKooDooKuConnect With a Mentor Commented:
I too was thinking of a simple function you could right yourself if you can't find that one your compiler already has built in.

But keep in mind that chaau's function assumes that there are no spaces or negative signs in your string.  Of course only you know what your data looks like, but a more general purpose function would look like this:

int64 my_atoi64(const char* txt)
{
  int64 res = 0;
  int neg = 0;
  while( *txt )
  {
    char ch = *txt;
    if( '0' <= ch && ch <= '9' )
      res = res*10 + ch - '0';
    else if( ch == '-' )
      neg = 1;
    txt++;
  }
  if( neg )
    res = res * -1;
  return res;
}

Open in new window

0
 
chaauCommented:
atoi64(). At least MS has it. See MSDN

Something like this:
int64 i = atoi(text);

Open in new window

0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Tried this (using WATCOM C) but got the following:

testfree64.c(153): Warning! W301: No prototype found for 'atoi64'
iff exist conslblm.lib then
wlink system nt  file testfree64.obj lib e:\src\opusrx\consults\conslb5m lib odb
c32.lib option CASEEXACT option undefsok option MAP option STACK=65535
WATCOM Linker Version 11.0c
Copyright by Sybase, Inc., and its subsidiaries, 1985, 2000.
All rights reserved.  Watcom is a trademark of Sybase, Inc.
loading object files
searching libraries
Warning! W1028: atoi64_ is an undefined reference


Any other ways to do this?
Thanks
Steve
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
chaauConnect With a Mentor Commented:
Perhaps your compiler does not have this function. You can easily get away without this function. The way you do this is like this:

loop through all the characters of the text value and multiply by 10

Here is a small function:

int64 my_atoi64(const char* txt)
{
 int64 res = 0;
 while (*txt) {
  res = res*10+(*txt)-'0';
  txt++;
 }
 return res;
}

Open in new window

This statement (*txt)-'0' is basically a conversion from char number to number, like '1' - '0' = 1, '2' - '0' = 2, etc.
0
 
Stephen KairysConnect With a Mentor Technical Writer - ConsultantAuthor Commented:
Chau and HooKooDooKu,

My data is totally predictiable: will never have a space or negative slgn.  I generate it via :

fprintf(out_fp, "%I64d\n", int64_df);

So I am going to try the simpler function.

Thanks!
-Steve
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Hi,
Just wondering what about the above raised a red flag. This is hardly the 1st time that I"ve accepted multiple solutions.
Thanks
Steve
0
 
chaauCommented:
Just letting you know that HooKooDooKu's solution will make the result negative if the '-' sign anywhere in the string. Also, his solution just ignores any non-numeric values, contradicting with the standard atoi function that stops at first non-numeric value, i.e. "1-cd12" would be converted by HooKooDooKu's function to -112, but the standard function will return 1. The more accurate solution will be:

int64 my_atoi64(const char* txt)
{
 int64 res = 0;
 int multiplier = 1;
 if(*txt == '-')
 {
   multiplier = -1;
   txt++;
 }
 while (*txt && '0' <= *txt && *txt <= '9') {
  res = res*10+(*txt)-'0';
  txt++;
 }
 return res * multiplier;
}

Open in new window

0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Thanks, Chau...I'll look at this in a bit...you make some valid points.
Steve
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
chaau-
I incoprated your logic into my function and it looks good although I will test a bit more.
Thank you...
-Steve

MODS: Can the points now be split 250/250 between Chaau and HooDooKu with Chaau getting the "best sol'n"?
Thanks
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
HooKooDooKu,
Ended up going with something prettyh close to your function....not much more programming effort involved.

Chau:
Your function seems to work for simple cases.

Thank you both!!
-Steve
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
To clarify: The below is the sol'n I more or less used. Thanks again.


int64 my_atoi64(const char* txt)
{
 int64 res = 0;
 int multiplier = 1;
 if(*txt == '-')
 {
   multiplier = -1;
   txt++;
 }
 while (*txt && '0' <= *txt && *txt <= '9') {
  res = res*10+(*txt)-'0';
  txt++;
 }
 return res * multiplier;
}
0
 
chaauCommented:
(looks like mine)
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Chaau- Yep, that's yours.
Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.