Solved

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

Posted on 2013-11-05
13
610 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

813 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

11 Experts available now in Live!

Get 1:1 Help Now