簡體   English   中英

在C ++中將輸出從十六進制轉換為十進制

[英]Converting an output from Hexadecimal to Decimal in c++

我試圖找到數組元素的內存地址,但是輸出結果卻是十六進制的。 如果您能告訴我如何將其轉換為小數輸出,我將不勝感激。

這是代碼,

#include<iostream>
using namespace std;

 int main()
  {
   int a[4];

   cout << "Address of a[0] = " << &a << endl;
   cout << "Address of a[1] = " << &a[1] << endl;
   cout << "Address of a[2] = " << &a[2] << endl;
   cout << "Address of a[3] = " << &a[3] << endl;

   cin.get();

   return 0;
  }

在C ++ 11中,您可以uintptr_tuintptr_t ,例如

cout << "Address of a[0] = " << static_cast<uintptr_t>(&a) << endl;

uintptr_t專門用於表示任何可能的指針,因此該解決方案是可移植的。

我不知道C ++ 98/03中的可移植解決方案,盡管通常(但並非總是) size_tsize_t 相關: 將指針轉換為整數


編輯

查看http://en.cppreference.com/w/cpp/types/integer以及C ++標准18.4.1標頭<cstdint>提要[cstdint.syn] ,它看起來像uintptr_t可選的 我不知道是否存在沒有定義它的實現,為什么不選擇不定義它。 歡迎任何意見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM