Novell Home

Sample Code - Printing and Converting 64-bit Integers

Novell Cool Solutions: Feature

Digg This - Slashdot This

Posted: 26 Oct 2005
 

This sample code shows how to print 64-bit integers and convert them to character strings.

You can also find this sample code at: http://forge.novell.com/modules/xfref_library/detail.php?reference_id=76

Sample Code

include <stdio.h>
#include <stdlib.h>

static void print64u( unsigned __int64 x) {
  unsigned __int64 quot = x / 1000;
  if ( quot != 0) {
    print64u( quot);
    printf( ".%03u", ( unsigned int)( x % 1000));
  }
  else {
    printf( "%u", ( unsigned int)( x % 1000));
  }
}
int main( void) {
  print64u( 0x100000000);
  return EXIT_SUCCESS;
}

#include <stdio.h>
#include <stdlib.h>

static int sprint64u( char* buffer, unsigned __int64 x) {
  unsigned __int64 quot = x / 1000;
  int chars_written;
  if ( quot != 0) {
    chars_written = sprint64u( buffer, quot);
    chars_written += sprintf( buffer + chars_written, ".%03u", ( unsigned int)( x % 1000));
  }
  else {
    chars_written = sprintf( buffer, "%u", ( unsigned int)( x % 1000));
  }
  return chars_written;
}
int main( void) {
  char buffer[ 32];
  sprint64u( buffer, 0x100000000ULL);
  puts( buffer);
  return EXIT_SUCCESS;
}


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell