在 C++ 中使用 char 数组作为数学数字
我正在尝试在 C++ 中生成 128 和 256 位整数,并注意到将 char**
转换为 int*
并将 int*
转换为 int
(以及向后)可用于将 char 数组转换为整数以及将整数转换为 char 数组。 另外,char* + int
工作得很好。
但是,当我尝试 char* + char* 时,编译器告诉我类型无效。有没有解决方法,或者我必须为操作员编写自己的函数?
例如:
int32_t intValue = 2147483647;
char *charPointer = *( char** ) &intValue;
charPointer += 2147483647;
charPointer += 2;
cout << ( *( int64_t* ) &charPointer ) << endl;
输出:4294967296
基本上,我所做的应该类似于以下内容:
int32_t intValue = 2147483647;
内存中的某处:
[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF 7F .. .. ] ( value, in hex )
然后:
char *charPointer = *( char** ) &intValue;
内存中的某处:
[ 58 59 5A 5B 5C 5D 5E 5F ] ( address, in hex )
[ .. .. 07 00 00 00 .. .. ] ( value, in hex )
然后:
charPointer += 2147483647;
我真的不知道这里发生了什么。 看起来它确实做了类似的事情:
[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF FE .. .. ] ( value, in hex )
那么:
charPointer += 2;
这里也一样。 像这样的事情:
[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. 00 00 00 00 01 .. ] ( value, in hex )
最后我只是打印它就好像它是一个8字节整数:
cout << ( *( int64_t* ) &charPointer ) << endl;
那么,任何人都可以解释为什么它不是添加的指针的值而是添加的值被指向?
I'm trying to make 128 and 256 bit integers in C++, and noticed casting char**
to int*
and int*
to int
(and backwards) can be used to convert char arrays to integers and integers to char arrays.
Also, char* + int
works fine.
However, when I try char* + char*
the compiler tells me the types are invalid. Is there any workaround for this, or will I have to write my own functions for the operators?
For example:
int32_t intValue = 2147483647;
char *charPointer = *( char** ) &intValue;
charPointer += 2147483647;
charPointer += 2;
cout << ( *( int64_t* ) &charPointer ) << endl;
output: 4294967296
Basically, what I do should be something like the following:
int32_t intValue = 2147483647;
somewhere in memory:
[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF 7F .. .. ] ( value, in hex )
then:
char *charPointer = *( char** ) &intValue;
somewhere in memory:
[ 58 59 5A 5B 5C 5D 5E 5F ] ( address, in hex )
[ .. .. 07 00 00 00 .. .. ] ( value, in hex )
then:
charPointer += 2147483647;
I honestly have no idea what happens here.
It seems like it does something like this though:
[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. FF FF FF FE .. .. ] ( value, in hex )
then:
charPointer += 2;
Same here.
Something like this:
[ 05 06 07 08 09 0A 0B 0C ] ( address, in hex )
[ .. .. 00 00 00 00 01 .. ] ( value, in hex )
And at last I just print it as if it were an 8 byte integer:
cout << ( *( int64_t* ) &charPointer ) << endl;
So, can anybody explain why it isn't the value of the pointer that is added but the value of what's being pointed to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些转换确实存在,但它们并没有按照您的想法进行。将指针转换为整数只是将其视为整数;它没有做任何实际的“数学”。例如,
char * s = "abcd"; int i = (int) s;
不会每次都给出相同的结果,因为s
和i
都只是字符串开始的内存地址。两者都与字符串的实际内容无关。类似地,
char* + int
只是获取偏移量。写成char * s = "abcd"; char * t = s + 2;
只是 char * s = "abcd"; 的另一种编写方式char * t = &(s[2]);;即s
是'a'
的内存位置,t
是'c'<的内存位置/code> (
s
,偏移两个char
宽度,即两个字节)。除了“指针算术”需要数学来计算字节偏移量并查找内存位置之外,没有进行任何实际的数学运算。char * + char *
没有意义:将两个内存位置“添加”在一起意味着什么?编辑:这是您添加到问题中的代码:
让我稍微扩展一下,这样就更清楚发生了什么:
这有意义吗?
These conversions exist, but they don't do what you think they do. Converting a pointer to an integer is just treating it as an integer; it's not doing any actual "math". For example,
char * s = "abcd"; int i = (int) s;
will not give the same result every time, becauses
andi
are both just the memory address that the string starts at. Neither has anything to do with the actual contents of the string.Similarly,
char* + int
is just taking an offset. To writechar * s = "abcd"; char * t = s + 2;
is just another way to writechar * s = "abcd"; char * t = &(s[2]);
; that is,s
is the memory location of the'a'
, andt
is the memory location of the'c'
(s
, offset by twochar
-widths, that is, two bytes). No actual math has taken place, except in the sense that "pointer arithmetic" requires math to compute byte offsets and find memory locations.char * + char *
doesn't make sense: what would it mean to "add" two memory locations together?Edit: Here is the code you added to your question:
Let me expand it a bit, so it's a bit clearer what's going on:
Does that make sense?