Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 3rd, 2004, 04:34 AM
Registered User
 
Join Date: Aug 2004
Location: Hyderabad, Andhra Pradesh, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default string to "long long" without using atoll

Can anybody help me with simple code to convert a char* to "long long" datatype as i cannnot use atoll( ) in my code because of old compiler

ultimatix
Reply With Quote
  #2 (permalink)  
Old August 3rd, 2004, 04:07 PM
Authorized User
 
Join Date: Feb 2004
Location: , , .
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You didn't say what compiler you are using. Different compilers have different ways of declaring and printing 64-bit integers. Here is a program that works with gnu c. Let me know if you need one for Borland or Microsoft compilers. I'll tell you what to change.

Code:
/* test long long conversions with gcc
 *
 * gcc has data type "long long" for 64-bit integers
 *
 * "%lld" is the conversion specifier for scanf/printf functions
 *
 */

#include <stdio.h>
#include <string.h>
long long my_atoll(char *);
int main()
{
  char buffer[256];
  long long value;
  long long value2;
  strcpy(buffer, "1234567890123456");
  sscanf(buffer, "%lld", &value);
  printf("\n\n");
  printf("   Value (from sscanf)    = %lld\n", value);
  value2 = my_atoll(buffer);
  printf("   Value (from my_atoll)  = %lld\n", value2);
  printf("\n\n");
  return 0;
}

/*
 * ascii-to-longlong conversion
 *
 * no error checking; assumes decimal digits
 *
 * efficient conversion: 
 *   start with value = 0
 *   then, starting at first character, repeat the following
 *   until the end of the string:
 *
 *     new value = (10 * (old value)) + decimal value of next character
 *
 */

long long my_atoll(char *instr)
{
  long long retval;
  int i;

  retval = 0;
  for (; *instr; instr++) {
    retval = 10*retval + (*instr - '0');
  }
  return retval;
}
Dave

Reply With Quote
  #3 (permalink)  
Old August 4th, 2004, 08:19 AM
Registered User
 
Join Date: Aug 2004
Location: Hyderabad, Andhra Pradesh, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dave

Thanks a lot. It worked on my HP-UX aCC compiler. Quite encouraging to get a reply as this was my first question asked in any forum :)


ultimatix
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
String legnth too long??? sithlrd ADO.NET 6 September 24th, 2013 05:16 PM
How to split a long string with tab delimited Andraw SQL Server 2005 27 July 17th, 2008 06:38 PM
Long Long int to bin walid C# 0 January 23rd, 2007 12:47 PM
how long can a string be? JesseM C# 0 July 5th, 2006 04:29 PM
Modifying long text in a long field sajsal Classic ASP Databases 1 February 20th, 2004 12:36 PM



All times are GMT -4. The time now is 08:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.