指针地址算术和十六进制/十进制转换 [英] Pointer address Arithmetic and Hex/Dec Conversion

查看:283
本文介绍了指针地址算术和十六进制/十进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 extern char etext end EDATA 。我还使用&<变量名称> 获得了变量的地址。两者都是十六进制指针地址。

I have a pointer address I obtained from the extern char etext, end and edata. I also obtained address of variables using &<Variable Name>. Both are hexadecimal pointer addresses.

我需要对这些地址进行十进制运算。我该怎么做?我需要将地址转换为十进制的int形式,以便通过减去地址来查找内存段的大小。

I need to do arithmetic on those addresses in decimal. How do I do so? I need to convert the address into an int in decimal so I can find size of memory segments by subtracting addresses.

推荐答案

数学是数学。不管你做什么基地都没关系。无论如何,计算机只能在基础2中工作。只有在输入或输出过程中才有基础。如果将它们解释为 char * 指针,则可以对指针进行Bytewise算术运算:

Math is math. It doesn't matter what base you do it on. The computer is working only in base 2 anyway. Only during input or output does base matter. Bytewise arithmetic on pointers is possible if you interpret them as char * pointers:

ptrdiff_t segmentSize = (char *)segmentEndAddress - (char *)segmentStartAddress;

printf("Segment size in base 10: %td\n", segmentSize);
printf("Segment size in base 16: %tx\n", segmentSize);
printf("Segment size in base  8: %to\n", segmentSize);

这篇关于指针地址算术和十六进制/十进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆