Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Aug 19, 2002

How to Print an __int64 Number in VC++


You can use the printf() function as in the following example:
 
#include <cstdio>

using namespace std;

int main()
{
  __int64 i64 = 4294967296;
  printf("%I64d\n", i64); //in decimal
  printf("%I64x\n", i64); //in hexa
  return 0;
}

You can also use the function Int64ToString(). The next example transforms an __int64 into a string in a specified radix:
 
#include <iostream>
#include <string>
#include <algorithm>

string Int64ToString(__int64 const& ri64, int iRadix=10)
{
  bool bNeg = (ri64 < 0);
  __int64 i64 = ri64;
  string ostrRes;
  bool bSpecial = false;
  if(true == bNeg)
  {
    i64 = -i64;
    if(i64 < 0)
      //Special case number -9223372036854775808 or _
0x8000000000000000
      bSpecial = true;
    ostrRes.append(1, '-');
  }
  int iR;
  do
  {
    iR = i64 % iRadix;
    if(true == bSpecial)
      iR = -iR;
    if(iR < 10)
      ostrRes.append(1, '0' + iR);
    else
      ostrRes.append(1, 'A' + iR - 10);
    i64 /= iRadix;
  }
  while(i64 != 0);
  //Reverse the string
  string::iterator it = ostrRes.begin();
  if(bNeg)
    it++;
  reverse(it, ostrRes.end());
  return ostrRes;
}

int main()
{
  __int64 i64 = 4294967296;
  cout << Int64ToString(i64) << endl; //in decimal
  cout << Int64ToString(i64, 16) << endl; //in hexa
  return 0;
}
George Anescu
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date