Solved

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

Posted on 2013-11-05
13
580 Views
Last Modified: 2013-11-12
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
0
Comment
Question by:Stephen Kairys
  • 8
  • 4
13 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39626273
atoi64(). At least MS has it. See MSDN

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

Open in new window

0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39626281
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
 
LVL 24

Assisted Solution

by:chaau
chaau earned 167 total points
ID: 39626328
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
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 333 total points
ID: 39627479
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
 
LVL 4

Assisted Solution

by:Stephen Kairys
Stephen Kairys earned 0 total points
ID: 39627571
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
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39628186
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 24

Expert Comment

by:chaau
ID: 39628318
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
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39628328
Thanks, Chau...I'll look at this in a bit...you make some valid points.
Steve
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39632352
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
 
LVL 4

Author Closing Comment

by:Stephen Kairys
ID: 39638300
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
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39643090
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
 
LVL 24

Expert Comment

by:chaau
ID: 39643134
(looks like mine)
0
 
LVL 4

Author Comment

by:Stephen Kairys
ID: 39643209
Chaau- Yep, that's yours.
Thanks.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now