When I do the following, I get this error:
当我做以下操作时,我得到这个错误:
../src/Sample.cpp:19: error: cast from \u2018UINT8*\u2019 to \u2018UINT8\u2019 loses precision
. . / src /样品。错误:从\u2018UINT8*\u2019到\u2018UINT8\u2019失去精度
#include <iostream>
using namespace std;
typedef unsigned char UINT8;
typedef unsigned int UINT32;
#define UNUSED(X) X=X
int main() {
UINT8 * a = new UINT8[34];
UINT32 b = reinterpret_cast<UINT8>(a);
UNUSED(b);
return 0;
}
How would I go about solving this. Keep in mind I am not trying to convert string to unsigned long, rather converting char* (the ADDRESS value ) to int.
我该怎么解决这个问题呢。请记住,我并不是要将字符串转换为无符号长,而是要将char*(地址值)转换为int。
Thanks
谢谢
Solution:
解决方案:
Turns out that this problem has to do with the pointer size. On a 32 bit machine, the pointer size is 32 bit, and for 64 bit machine is of course 64. The above wont work on a 64 bit machine, but will on a 32 bit machine. This will work on a 64 bit machine.
事实证明这个问题与指针大小有关。在32位机器上,指针大小是32位,64位机器当然是64位。上述方法不适用于64位机,但适用于32位机。这将在64位机器上工作。
#include <iostream>
#include <stdint.h>
using namespace std;
typedef uint8_t UINT8;
typedef int64_t UINT32;
#define UNUSED(X) X=X
int main() {
UINT8 * a = new UINT8[34];
UINT32 b = reinterpret_cast<UINT32>(a);
UNUSED(b);
return 0;
}
3 个解决方案
#1
3
Assuming that sizeof(int) == sizeof(void*) you can use this code to convert:
假设sizeof(int) = sizeof(void*),您可以使用此代码进行转换:
int b = *reinterpret_cast<int*>(&a);
or variations thereof. I think static_cast will work too.
或变化。我认为static_cast也可以。
Of course a has to be l-value (able to be assigned to) to get the address of it. For non-l-values, you need to use a function and the good old union trick will work:
当然a必须是l-value(能够被分配到)才能得到它的地址。对于非l值,您需要使用一个函数,好的老联合技巧将会奏效:
int Pointer2Int (void* p)
{
union { void* p; int i; } converter;
converter.p = p;
return converter.i;
}
#2
1
This isn't a good idea, but the line should read:
这不是一个好主意,但这句话应该是:
UINT32 b = reinterpret_cast<UINT32>(a);
reinterpret_cast
takes the type of the destination type as the template parameter.
reinterpretation t_cast将目标类型的类型作为模板参数。
With the correct type g++ will tell you that it isn't a good idea:
正确的类型g++会告诉你这不是个好主意:
error: invalid cast from type ‘char’ to type ‘int’
see Cthutu's answer for a more correct approach.
请参阅Cthutu的答案,以获得更正确的方法。
#3
0
An option would be to :
一种选择是:
int *a; char b[64]; int c;
int *;字符b[64];int c;
a = (int*) malloc (sizeof(int));
a = (int*) malloc (sizeof(int));
sprintf(b, "%d", a);
sprintf(b,“% d”);
c = atoi(b);
c = atoi(b);
#1
3
Assuming that sizeof(int) == sizeof(void*) you can use this code to convert:
假设sizeof(int) = sizeof(void*),您可以使用此代码进行转换:
int b = *reinterpret_cast<int*>(&a);
or variations thereof. I think static_cast will work too.
或变化。我认为static_cast也可以。
Of course a has to be l-value (able to be assigned to) to get the address of it. For non-l-values, you need to use a function and the good old union trick will work:
当然a必须是l-value(能够被分配到)才能得到它的地址。对于非l值,您需要使用一个函数,好的老联合技巧将会奏效:
int Pointer2Int (void* p)
{
union { void* p; int i; } converter;
converter.p = p;
return converter.i;
}
#2
1
This isn't a good idea, but the line should read:
这不是一个好主意,但这句话应该是:
UINT32 b = reinterpret_cast<UINT32>(a);
reinterpret_cast
takes the type of the destination type as the template parameter.
reinterpretation t_cast将目标类型的类型作为模板参数。
With the correct type g++ will tell you that it isn't a good idea:
正确的类型g++会告诉你这不是个好主意:
error: invalid cast from type ‘char’ to type ‘int’
see Cthutu's answer for a more correct approach.
请参阅Cthutu的答案,以获得更正确的方法。
#3
0
An option would be to :
一种选择是:
int *a; char b[64]; int c;
int *;字符b[64];int c;
a = (int*) malloc (sizeof(int));
a = (int*) malloc (sizeof(int));
sprintf(b, "%d", a);
sprintf(b,“% d”);
c = atoi(b);
c = atoi(b);