2016年4月14日 星期四

printf 列印 64bit int


//======================================
一個另我快瘋掉的型別
因有一組程式會共用
同時在 Windows x86(32bit)/NIOS(32bit)/Linux(32bit)/Linux(64bit)
下共同執行
所以...型別就出現問題了
(還好沒有想要另弄到 Win64下...)
那 printf 要怎搭配?

Windows(32bit)
 int  是 4個 byte
 long 是 4個 byte <== 注意
 long long 是 8個 byte
 DWORD 是 4個 byte =>  unsigned long
 wchar_t 是 2個 byte

Linux(64bit)
 int 是 4個 byte
 long 是 8個 byte  <== 注意
 long long 是 8個 byte
 DWORD 必需也是 4個 byte =>  unsigned int  


 wchar_t 是 4個 byte

for Linux x64 gc++


_T("%x")    // => INT32   int                 4byte
_T("%X")    // => INT32   int                 4byte
_T("%lx")   // => void *, INT64, unsigned long      8byte
_T("%lX")   // => void *, INT64, unsigned long      8byte
_T("%llx")  // => unsigned long long, long long     8byte
_T("%llX")  // => unsigned long long, long long     8byte
_T("%d")    // => INT32   int                 4byte
_T("%u")    // => UINT32  unsigned int        4byte
_T("%ld")   // => INT64   long                8byte
_T("%lu")   // => UINT64  unsigned long       8byte
_T("%lld")  // => UINT64  long long           8byte
_T("%llu")  // => UINT64  unsigned long long  8byte


  char *pargv = (char *)0xEFFFFFFFFFFFFFFE;
  printf( "   p=>  %p \n", pargv);
  printf( "  lx=>  %lx \n", (unsigned long)pargv);
  printf( "  lX=>  %lX \n", (unsigned long)pargv);
  printf( "  lu=>  %lu \n", (unsigned long)pargv);
   p=>  0xeffffffffffffffe 
  lx=>  effffffffffffffe 
  lX=>  EFFFFFFFFFFFFFFE 
  lu=>  17293822569102704638 
 
//======================================
 unsigned int nX = 123;
 printf( "   p=>  %p \n", &nX);
 printf( "   p=>  %08p \n", &nX);
 printf( "   p=>  %016p \n", &nX);

 printf( "   p=>  %p \n", nX);
 printf( "   p=>  %08p \n", nX);
 printf( "   p=>  %016p \n", nX);
 printf( "   p=>  %010p \n", nX);
 printf( "   p=>  %018p \n", nX);
p=> 0x7fffffffdef0 p=> 0x7fffffffdef0 p=> 0x007fffffffdef0 p=> 0x7b p=> 0x00007b p=> 0x0000000000007b
   p=>  0x0000007b 
   p=>  0x000000000000007b 


//======================================
請參考這一篇
一個長整數各自表述 (Size of long integer may vary in 64-bit systems)

其實當在做跨平台的軟體時,
本來就要先自行檢查一次
看各種型別的 sizeof
尤其是在寫通訊程式或硬體相關程式
都要特別注意

//======================================
unsigned long nBufData = 123456; 

printf("lu  %lu\n", nBufData);

//====================================== "%lli" for int64_t
"%llu" for uint64_t
"%llx" for hex
"%llX" for HEX

UINT64 nBuffer = 140737320412160;  // 0x7FFFF5FD6400


printf("lli  %lli\n", nBuffer);
printf("llu  %llu\n", nBuffer);
printf("lld  %lld\n", nBuffer);
printf("llx  %llx\n", nBuffer);
printf("llX  %llX\n", nBuffer);
printf("0x%x%x\n", *(((int*)(&nBuffer))+1), nBuffer);

printf("lli  %016lli\n", nBuffer);
printf("llu  %016llu\n", nBuffer);
printf("lld  %016lld\n", nBuffer);
printf("llx  %016llx\n", nBuffer);
printf("llX  %016llX\n", nBuffer);

printf("lli  %016lli\n", nBuffer);
printf("llu  %016llu\n", nBuffer);
printf("lld  %016lld\n", nBuffer);
printf("llx  %016llx\n", nBuffer);
printf("llX  %016llX\n", nBuffer);

//-----------------------------------------------------------
 // for Linux x64 gc++
  char *pargv = (char *)0xEFFFFFFFFFFFFFFE;
  printf( "   p=>  %p \n", pargv);
  printf( "  lx=>  %lx \n", (unsigned long)pargv);
  printf( "  lX=>  %lX \n", (unsigned long)pargv);

  printf( "  lu=>  %lu \n", (unsigned long)pargv);
   p=>  0xeffffffffffffffe 
  lx=>  effffffffffffffe 
  lX=>  EFFFFFFFFFFFFFFE 
  lu=>  17293822569102704638 

//-----------------------------------------------------------
  void *pargv = (void *)0xEFFFFFFFFFFFFFFE;
  printf( "   p=>  %p \n", pargv);
  printf( "   x=>  %x \n", pargv);
  printf( "  lx=>  %lx \n", pargv);
  printf( " llx=>  %llx \n", pargv);
  printf( "   X=>  %X \n", pargv);
  printf( "  lX=>  %lX \n", pargv);
  printf( " llX=>  %llX \n", pargv);
  printf( "  lu=>  %lu \n", (unsigned long)pargv);

  printf( " llu=>  %llu \n", (unsigned long)pargv);

   p=>  0xeffffffffffffffe 
   x=>  fffffffe 
  lx=>  effffffffffffffe 
 llx=>  effffffffffffffe 
   X=>  FFFFFFFE 
  lX=>  EFFFFFFFFFFFFFFE 
 llX=>  EFFFFFFFFFFFFFFE 
  lu=>  17293822569102704638 

 llu=>  17293822569102704638 

//-----------------------------------------------------------
// argv char ** 0x7fffffffe028

int main(int argc,char *argv[])
{
 printf( "   p=>  %p \n", argv);
 printf( "   x=>  %x \n", argv);
 printf( "  lx=>  %lx \n", argv);
 printf( " llx=>  %llx \n", argv);
 printf( "   X=>  %X \n", argv);
 printf( "  lX=>  %lX \n", argv);
 printf( " llX=>  %llX \n", argv);
 printf( "  lu=>  %lu \n", (unsigned long)argv);
 printf( " llu=>  %llu \n", (unsigned long)argv);
}

   p=>  0x7fffffffe028 
   x=>  ffffe028 
  lx=>  7fffffffe028 
 llx=>  7fffffffe028 
   X=>  FFFFE028 
  lX=>  7FFFFFFFE028 
 llX=>  7FFFFFFFE028 
  lu=>  140737488347176 
 llu=>  140737488347176 

//-----------------------------------------------------------





沒有留言:

張貼留言