Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 3rd, 2004, 03: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, 03: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, 07: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


Similar Threads
Thread Thread Starter Forum Replies Last Post
String legnth too long??? sithlrd ADO.NET 6 September 24th, 2013 04:16 PM
How to split a long string with tab delimited Andraw SQL Server 2005 27 July 17th, 2008 05: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 03:29 PM
Modifying long text in a long field sajsal Classic ASP Databases 1 February 20th, 2004 12:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.