Cezar Mdlosci
Cezar Mdlosci

Reputation: 67

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.

Upvotes: 0

Views: 158

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117308

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);

Upvotes: 1

Related Questions