C++ - DWORD Decimal to DWORD Hexadecimal [duplicate] - c++

This question already has answers here:
C++ cout hex values?
(10 answers)
Closed 5 years ago.
How can you convert a decimal to hexadecimal in C++? I've had success with ultoa but that gives you a char (and using (DWORD)Buffer to std::cout just ends in gibberish). Also i need to save the new hexadecimal in a DWORD again.
My Code so far:
//Vars
char Buffer[33];
// Client.dll
DWORD d_clientDll = (DWORD)GetModuleHandleA("client.dll");
_ultoa(d_clientDll, Buffer, 16);
std::cout << Buffer << std::endl;
std::cout << d_clientDll << std::endl;

Did you try std::hex ?
std::cout << std::hex << d_clientDll << std::endl

Related

Disproving Determinism via c++ [duplicate]

This question already has answers here:
Two different values at the same memory address
(7 answers)
C/C++ changing the value of a const
(18 answers)
Closed 5 months ago.
friends.
Recently I experimented a bit a C++ constants.
The code is:
#include <iostream>
int main() {
const int c = 1;
const int* ptr = &c;
int* tmp = const_cast<int*>(ptr);
*tmp = 5;
std::cout << &c << " " << ptr << " " << tmp << "\n";
std::cout << c << " " << *ptr << " " << *tmp;
}
I have investigated assembly code at godbolt: https://godbolt.org/z/7e3o7bWrs
The assembly code seems like doing what I wrote, exactly moving address of c into tmp variable and changes variable at this address.
Can you please tell me, why there is could be two different values at the same addresses?
Thank you.

why cout output memory address not string? [duplicate]

This question already has answers here:
How to print array of LPCTSTR in c++?
(4 answers)
How to initialize and print a std::wstring?
(6 answers)
Closed 3 years ago.
i am tring to use c++ to get display device info ,but cout give me some memory address ,how can i get string value ? output like :00CFF4B4:00CFF5F8,
i am using visual studio 2019
#include <iostream>
#include <windows.h>
int main()
{
DISPLAY_DEVICE d;
d.cb = sizeof(DISPLAY_DEVICE);
int device_num = 0;
while (EnumDisplayDevices(NULL, device_num, &d, 0))
{
std::cout << d.DeviceName << ":" << d.DeviceID<<std::endl;
device_num++;
}
return 0;
}
I'm assuming you're using the W version of DISPLAY_DEVICE (DISPLAY_DEVICEW) which I believe is the default since at least Visual Studio 2015 and later (the compiler defines UNICODE).
The DeviceName and DeviceID members of DISPLAY_DEVICEW are WCHAR (a macro for wchar_t), so you need to use wcout instead of cout:
std::wcout << d.DeviceName << ":" << d.DeviceID << std::endl;

How to print Point (openCV) value? [duplicate]

This question already has answers here:
No console output on cout
(7 answers)
Closed 5 years ago.
Is this possible to print Point value?
For the example, I have a Point like this:
Point coordinate = Point(150,300);
And I want to show its value. I tried several ways, like:
first:
cout << coordinate << "\n";
second:
cout << coordinate.x << coordinate.y << "\n";
I also try the suggestion to flush it, become:
std::cout << coordinate << std::endl;
But none of those are work in my case. Is there any suggestion? Thanks for your help.
Ps. I work with opencv 3 and c++
std::cout << coordinate.x << "," << coordinate.y << std::endl;

Elements in array keep changing unintentionally [duplicate]

This question already has answers here:
Returning local data from functions in C and C++ via pointer
(13 answers)
Closed 5 years ago.
So I have two arrays which hold players x, y and z co-ordinates, however each time I call them they change their values the first time they are printed to the console they display the correct result however subsequent prints to the screen yield very small numbers,
HisPosition = GetPlayerPosition(aPlayer);
std::cout << "TheirPos=" << std::dec << HisPosition[0] << std::endl;
std::cout << "TheirPos1=" << std::dec << HisPosition[0] << std::endl;
if(!isnan(HisPosition[0])){
std::cout << "TheirPos2=" << std::dec << HisPosition[0] << std::endl;
Example console output:
TheirPos=440
TheirPos1=1.7118e-037
TheirPos2=1.7118e-037
They are defined like so:
float* HisPosition;
And GetPlayerPosition:
float* GetPlayerPosition(DWORD dwBase)
{
float result [3];
DWORD aXPos = dwBase + 0x134;
DWORD aYPos = dwBase + 0x138;
DWORD aZPos = dwBase + 0x13C;
result[0] = Read<float>(aXPos);
result[1] = Read<float>(aYPos);
result[2] = Read<float>(aZPos);
return result;
}
aPlayer stands for the address in memory of the player I am trying to fetch the offsets are correct as on the first console print the position is correct.
I'm new to using arrays in C++ and would appreciate any guidance.
return result; returns a dangling pointer to local, stack-allocated array that gets vanished when goes out of scope.

lexical_cast strtof strtold lose accuracy? [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 6 years ago.
Here:
#include <iostream>
#include <cstdlib>
#include <boost/lexical_cast.hpp>
int main(void) {
const char * str = "277499.84";
std::cout << boost::lexical_cast<double>(str) << std::endl;
std::cout << strtof(str, NULL) << std::endl;
std::cout << strtold(str, NULL) << std::endl;
std::cout << atof(str) << std::endl;
return 0;
}
output:
277500
277500
277500
277500
Why the output are not 277499.84?
It's not the operations themselves losing accuracy, but the output.
You can use the I/O manipulator std::setprecision to control the numeric precision. The following will use the full precision of a double (assuming the stream is set for decimal output).
double value = boost::lexical_cast<double>(str);
std::cout << std::setprecision( std::numeric_limits<double>::digits10 + 1 ) << value;
Or you can use std::ios_base::precision. This is useful if you want to restore the precision to the original value after.
auto old_precision = cout.precision( std::numeric_limits<double>::digits10 + 1 );
cout << value;
cout.precision( old_precision );