简体   繁体   中英

Convert char pointer hex to a string and save in text file C++

I create a dll that is injected in a process and return a hex value for example : 570AC400. I have a function of type __int64 GetLocalPlayer_EX() that return this hex values, but text saved in txt return strange string like @*░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

PS: if I use printf("LocalPlayer =%I64X\\n", ptr); , the return hex value is correct.

You get the raw int back from the function. You shouldn't cast it to a char*. Try this:

__int64 rv = GetLocalPlayer_EX();

printf("LocalPlayer = %ld\n", rv);
printf("LocalPlayer = %X\n", rv);

but I wonder if the signature of the function is correct. Does it really return __int64 and not a ClientPlayer* ?

Edit: Since it seems to be a pointer in disguise,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM