繁体   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