C++ 将字符指针十六进制转换为字符串并保存在文本文件C++;

C++ 将字符指针十六进制转换为字符串并保存在文本文件C++;,c++,c++11,C++,C++11,我创建了一个在进程中注入的dll,并返回一个十六进制值,例如:570AC400。我有一个类型为\uu int64 GetLocalPlayer\u EX()的函数,它返回这个十六进制值,但保存在txt中的文本返回奇怪的字符串,如@*░B char *ptr = reinterpret_cast<char*>(GetLocalPlayer_EX());//GetLocalPlayer_EX() is function return hex value std::string str(p

我创建了一个在进程中注入的dll,并返回一个十六进制值,例如:570AC400。我有一个类型为
\uu int64 GetLocalPlayer\u EX()
的函数,它返回这个十六进制值,但保存在txt中的文本返回奇怪的字符串,如@*░B

char *ptr = reinterpret_cast<char*>(GetLocalPlayer_EX());//GetLocalPlayer_EX() is function return hex value
std::string str(ptr);

printf("LocalPlayer = %s\n", ptr);//print to test, but return strange string like  @*░B should be 570AC400


void WriteLogFile(const char* szString)//convert char hex to string and save in txt
{


    FILE* pFile = fopen("C:\\TesteArquivo\\TesteFile.txt", "a");
    fprintf(pFile, "%s\n", szString);
    fclose(pFile);


}

WriteLogFile(vOut); // call function to save txt file
char*ptr=reinterpret_cast(GetLocalPlayer_EX())//GetLocalPlayer_EX()是函数返回十六进制值
std::字符串str(ptr);
printf(“LocalPlayer=%s\n”,ptr)//打印以进行测试,但返回奇怪的字符串,如@*░B应为570AC400
void WriteLogFile(const char*szString)//将字符十六进制转换为字符串并保存在txt中
{
FILE*pFile=fopen(“C:\\TesteArquivo\\TesteFile.txt”,“a”);
fprintf(pFile,“%s\n”,szString);
fclose(pFile);
}
可写文件(vOut);//调用函数保存txt文件

PS:如果我使用
printf(“LocalPlayer=%I64X\n”,ptr),返回的十六进制值是正确的。

从函数中返回原始整数。你不应该把它转换成字符*。试试这个:

__int64 rv = GetLocalPlayer_EX();

printf("LocalPlayer = %ld\n", rv);
printf("LocalPlayer = %X\n", rv);
但是我想知道函数的签名是否正确。它真的返回
\uu int64
而不是
客户端播放器*

编辑: 因为它似乎是一个伪装的指针

char *ptr = reinterpret_cast<char*>(GetLocalPlayer_EX());
printf("%p\n", ptr);
char*ptr=reinterpret_cast(GetLocalPlayer_EX());
printf(“%p\n”,ptr);

如果更改为
printf(“LocalPlayer=%s\n”,str),会发生什么情况?(对不起,放错了东西。)像我说的那样返回陌生人字符串=/是Int64,但返回地址(十六进制值),不想只是打印,我想转换为字符串,打印只是为我检查是否正确转换编辑了我的答案以反映这一点。我现在得到了,在我的函数WriteLogFile中我更改了fprintf(pFile,“%s\n”,szString);到fprintf(pFile,“%I64X\n”,szString);好的,但是既然您打印的是指针地址而不是字符串或数字,为什么不使用%p呢?顺便问一下,您有描述ClientPlayer的标题吗?我想您正在使用DLL进行此导出<代码>外部“C”\u declspec(dllexport)ClientPlayer*GetLocalPlayer\u EX()