C++/CPP Reading an added hex address - c++

I'm still relatively new-ish to CPP but I can't find any source that has my issue. My IDE is MSVC 2017 Preview and my desired outcome is to add two hex addresses together, then read the address' value. I'm not sure why but it's not playing nicely with adding hex numbers. I'll give you my current example:
int number;
ReadProcessMemory(pHandle, (void*)(0x37c90000 + 0xE29FE8), &number, sizeof(number), 0);
std::cout << number << " for " << std::hex << (0x37c90000 + 0xE29FE8) << std::endl;
ReadProcessMemory(pHandle, (void*)(0x38ab9fe8), &number, sizeof(number), 0);
std::cout << number << " for " << std::hex << (0x38ab9fe8) << std::endl;
std::cout << "a = " << (0x37c90000 + 0xE29FE8) << std::endl;
std::cout << "b = " << (0x38ab9fe8) << std::endl;
My predicted outcome of this code would be that both ReadProcessMemory should get the same exact same value, but instead only the second ReadProcessMemory (with the already added hex value) returns properly.
Furthermore, cout-ing A and B report the exact same address. If that's the case, why is ReadProcessMemory throwing a tantrum and reporting a negative number on the first ReadProcessMemory?
Here's my outcome with the code above:
-331287296 for 38ab9fe8
ec40f500 for 38ab9fe8
a = 38ab9fe8
b = 38ab9fe8
Apologies but I truly can't figure out why they're different in any way.

No that seems about right. 0xec40f500 is the unsigned hexadecimal representation of the decimal signed value -331287296. Please learn about two's complement representation of negative numbers.
The problem is, I believe, the std::hex manipulator, which is sticky. If you in the second case use std::dec both numbers will be displayed as decimal:
// Second output
std::cout << std::dec << number << " for " << std::hex << (0x38ab9fe8) << std::endl;
// ^^^^^^^^
// Display *all* following numbers as decimal
Or use std::hex in the first output:
// First output
std::cout << std::hex << number << " for " << (0x37c90000 + 0xE29FE8) << std::endl;
// ^^^^^^^^
// Display *all* following numbers as hexadecimal
Of course, if you intend for number to be unsigned, you need to explicitly say so when defining the variable. Perhaps it would be easier for you to see the connection between 0xec40f500 and the unsigned decimal value 3963680000?

Related

What's the difference between llround() and round() function in C++?

so the reason why I have this question is that I was doing a problem in which we had to round the final answer. Now,
cout << round(answer); //this didn't pass one test case
//the following block of code passed all test cases
if (answer-(int)answer>0.5) cout << int(answer)+1;
else cout << (int)answer;
cout << llround(answer); //this passed all test cases
So, what is the difference between round() and llround() for this type of behavior? If the answer variable would have been so big to not fit in the int type, then if-else block of code shouldn't have worked either. So what am I missing?
Thank you!
round() returns a floating point value, while your alternatives return integer value. With the default formatting of cout, there should be no difference in the output, but if you have changed the formatting, there might be a difference:
double answer = 1.5;
cout << round(answer) << " " << llround(answer) << "\n"; // Prints 2 2
cout << std::fixed << setprecision(5);
cout << round(answer) << " " << llround(answer) << "\n"; // Prints 2.00000 2

Printf and fstream operator << displaying different values. Why?

I'm trying to get the values of x and y coordinates of two eyes. I detect it using opencv XML file, and in the console 2 different x values appear from printf() while the text file I save with operator<< displays 1 value. Why is this so?
printf("X = %o,Y = %o\n", eyes[j].x, eyes[j].y);
ofstream coordinates;
coordinates.open("C:/Users/dougl/Desktop/Coordinates.txt");
coordinates << "X = " << eyes[j].x << "\n" << "Y = " << eyes[j].y;
#include <iostream>
using std::cout;
using std::endl;
using std::oct;
using std::hex;
int main()
{
long int pos_value = 12345678;
cout << "The decimal value 12345678 is printed out as" << endl;
cout << "octal: " << oct << pos_value << endl;
cout << "hexadecimal: " << hex << pos_value << endl << endl;
return 0;
}
Printf showing the Unsigned Octal number for integer using: %o format.
https://www.geeksforgeeks.org/format-specifiers-in-c/amp/
Stream operator<< overload work as per the data type of the value passed.
So, to print octal value you need to formatting (std::oct) :
cout << "octal: " << oct << pos_value << endl;
Reference:http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node83.html
You are telling printf() to output the integers in octal form, whereas operator<< outputs the integers in decimal form by default.
To make the two outputs match, you need to either:
change %o to %d or %u, depending on whether the x and y values are signed or unsigned, respectively.
use the std::oct I/O manipulator with operator<<.

How to print complex numbers in C++

I am playing around with classes in C++. Currently I am working on a class for complex numbers and want to be able to print them in the following format: -2+3i, 1-4i. That means, that I want the real part to have only a sign if it is negative. In contrast the imaginary part should have always a sign whether it is positive or negative.
I tried the following which did not work as expected:
inline void Complex::print() const {
std::cout << x;
std::cout << std::showpos << y << "i" << std::endl;
}
This method prints also for the real part the sign if it is positive. Why does std::showpos affect the first line?
Is there any better way to do that?
showpos is "sticky", and applies to every following number until it's changed back with noshowpos.
You can use showpos with a local stream to avoid messing with std::cout:
inline void Complex::print() const {
std::ostringstream y_stream;
y_stream << showpos << y;
std::cout << x
<< y_stream.str() << 'i'
<< std::endl;
}
When the showpos format flag is set, a plus sign (+) precedes every non-negative numerical value inserted into the stream (including zeros). This flag can be unset with the noshowpos manipulator.
Minor change in your code:
inline void Complex::print() const {
std::cout << std::noshowpos << x;
std::cout << std::showpos << y << "i" << std::endl;
}
That's because the std::showpos flag affects every number inserted into the stream. (http://www.cplusplus.com/reference/ios/showpos/)
Have you tried using if statements?
if (x > 0)
{
std::cout << "+";
}
If you never use std::noshowpos, std::cout will keep the showpos flag, so that next time you call print() it affects x (and any other number you ever print with std::cout in your program).
So either use std::noshowpos directly after printing y:
std::cout << std::showpos << y << std::noshowpos << "i" << std::endl;
or directly before printing x:
std::cout << std::noshowpos << x;

why i am getting output blank?

why I am getting output blank? pointers are able to modify but can't read.why?
#include <iostream>
using namespace std;
int main(){
int a = 0;
char *x1,*x2,*x3,*x4;
x1 = (char *)&a;
x2 = x1;x2++;
x3 = x2;x3++;
x4 = x3;x4++;
*x1=1;
*x2=1;
*x3=1;
*x4=1;
cout <<"#" << *x1 << " " << *x2 << " " << *x3 << " " << *x4 << "#"<<endl ;
cout << a << endl;
}
[Desktop]👉 g++ test_pointer.cpp
[Desktop]👉 ./a.out
# #
16843009
I want to read the value of integer by using pointers type of char.
so i can read byte by byte.
You're streaming chars. These get automatically ASCII-ised for you by IOStreams*, so you're seeing (or rather, not seeing) unprintable characters (in fact, all 0x01 bytes).
You can cast to int to see the numerical value, and perhaps add std::hex for a conventional view.
Example:
#include <iostream>
#include <iomanip>
int main()
{
int a = 0;
// Alias the first four bytes of `a` using `char*`
char* x1 = (char*)&a;
char* x2 = x1 + 1;
char* x3 = x1 + 2;
char* x4 = x1 + 3;
*x1 = 1;
*x2 = 1;
*x3 = 1;
*x4 = 1;
std::cout << std::hex << std::setfill('0');
std::cout << '#' << std::setw(2) << "0x" << (int)*x1
<< ' ' << std::setw(2) << "0x" << (int)*x2
<< ' ' << std::setw(2) << "0x" << (int)*x3
<< ' ' << std::setw(2) << "0x" << (int)*x4
<< '#' << '\n';
std::cout << "0x" << a << '\n';
}
// Output:
// #0x01 0x01 0x01 0x01#
// 0x1010101
(live demo)
Those saying that your program has undefined are incorrect (assuming your int has at least four bytes in it); aliasing objects via char* is specifically permitted.
The 16843009 output is correct; that's equal to 0x01010101 which you'd again see if you put your stream into hex mode.
N.B. Some people will recommend reinterpret_cast<char*>(&a) and static_cast<int>(*x1), instead of C-style casts, though personally I find them ugly and unnecessary in this particular case. For the output you can at least write +*x1 to get a "free" promotion to int (via the unary + operator), but that's not terribly self-documenting.
* Technically it's something like the opposite; IOStreams usually automatically converts your numbers and booleans and things into the right ASCII characters to appear correct on screen. For char it skips that step, assuming that you're already providing the ASCII value you want.
Assuming an int is at least 4 bytes long on your system, the program manipulates the 4 bytes of int a.
The result 16843009 is the decimal value of 0x01010101, so this is as you might expect.
You don't see anything in the first line of output because you write 4 characters of a binary value 1 (or 0x01) which are invisible characters (ASCII SOH).
When you modify your program like this
*x1='1';
*x2='3';
*x3='5';
*x4='7';
you will see output with the expected characters
#1 3 5 7#
926233393
The value 926233393 is the decimal representation of 0x37353331 where 0x37 is the ASCII value of the character '7' etc.
(These results are valid for a little-endian architecture.)
You can use unary + for converting character type (printed as symbol) into integer type (printed as number):
cout <<"#" << +*x1 << " " << +*x2 << " " << +*x3 << " " << +*x4 << "#"<<endl ;
See integral promotion:
Have a look at your declarations of the x's
char *x1,*x2,*x3,*x4;
these are pointers to chars (characters).
In your stream output they are interpreted as printable characters.
A short look into the ascii-Table let you see that the lower numbers are not printeable.
Since your int a is zero also the x's that point to the individual bytes are zero.
One possibility to get readeable output would be to cast the characters to int, so that the stream would print the numerical representation instead the ascii character:
cout <<"#" << int(*x1) << " " << int(*x2) << " " << int(*x3) << " " << int(*x4) << "#"<<endl ;
If I understood your problem correctly, this is the solution
#include <stdio.h>
#include <iostream>
using namespace std;
int main(){
int a = 0;
char *x1,*x2,*x3,*x4;
x1 = (char*)&a;
x2 = x1;x2++;
x3 = x2;x3++;
x4 = x3;x4++;
*x1=1;
*x2=1;
*x3=1;
*x4=1;
cout <<"#" << (int)*x1 << " " << (int)*x2 << " " << (int)*x3 << " " << (int)*x4 << "#"<<endl ;
cout << a << endl;
}

std::stringstream strange behaviour

Some background information, for a homework assignment I had to write a polish notation calculator using binary trees, for this to work I had to parse command line input so that it would properly build the binary tree and then go over it to give a valid answer to the mathematical expression that was entered.
For the parsing I used a std::stringstream so that I would easily be able to convert the std::string I was handed into a valid float (or integer, double). The issue I ran across was the following code, which has the error showcased and how I solved the issue. I was hoping that somebody where would be able to tell me if I was doing something wrong and .clear() is not correct, or if this is a bug in the standard library in the way it handles this particular input (only happens for + and -).
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string mystring("+");
int num;
char op;
std::stringstream iss(mystring);
iss >> num;
// Seems it is not a number
if (iss.fail()) {
// This part does not work as you would expect it to
// We clear the error state of the stringstream
iss.clear();
std::cout << "iss fail bit: " << iss.fail() << std::endl;
iss.get(op);
std::cout << "op is: " << op << " iss is: " << iss.str() << std::endl;
std::cout << "iss fail bit: " << iss.fail() << std::endl;
// This however works as you would expect it to
std::stringstream oss(iss.str());
std::cout << "oss fail bit: " << oss.fail() << std::endl;
oss.get(op);
std::cout << "op is: " << op << " oss is: " << oss.str() << std::endl;
std::cout << "oss fail bit: " << oss.fail() << std::endl;
} else {
// We got a number
}
}
Sample output from the program:
iss fail bit: 0
op is: iss is: +
iss fail bit: 1
oss fail bit: 0
op is: + oss is: +
oss fail bit: 0
Maybe you guys will see something I missed, or if this is indeed a bug higher up beyond my program, in which case pointers as to where to report this would be greatly appreciated.
When you say:
iss.clear();
std::cout << "iss fail bit: " << iss.fail() << std::endl;
iss.get(op);
you are trying to read something that has already been read. You need to reset the streams read pointer:
iss.clear();
iss.seekg(0); // start again
std::cout << "iss fail bit: " << iss.fail() << std::endl;
iss.get(op);