View Single Post
  #2 (permalink)  
Old August 3rd, 2004, 03:07 PM
davekw7x davekw7x is offline
Authorized User
 
Join Date: Feb 2004
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