5aLIVE
June 30th, 2009, 12:09
I've been taking my first steps in learning C++ and writing little programs as I go to further my understanding.
I've written this simple console program below :
The above if statement is displaying the 'found key' message when *pchar is equal to 0x42, 0x43, 0x47, 0x48, 0x4A.
Surely the message showed only be displayed when the pointer to the unsigned character is 0x42 (ASCII character 'B').
All of the above hex values have bit positon two set to '1' which seems to be influencing this although it shouldn't.
What could the problem be?
I've written this simple console program below :
Code:
#include <iostream>
#include <iomanip>
using namespace std;
using std::cout;
using std::endl;
using std:: setfill;
using std::setw;
int main(){
unsigned char* pchar;
unsigned char hexchar = 0x41;
int i = 0;
pchar =&hexchar;
cout << std::hex;
cout << setfill('0');
cout << "The value of the hex variable is ";
cout << "\nHex value of hexchar is " << setw(4) << *pchar;
cout << "\nThe size of hexchar variable is :" << sizeof(*pchar) << "bytes(s)"
<< endl;
cout << hex << *pchar << endl;
for(i=0;i<10;i++)
{
cout << "\nThe hex value is currently set at : " << *pchar;
if((*pchar & 0x42) == 0x42)
cout << "\nYou have found the 'key'";
(*pchar)++;
}
cout << endl;
return 0;
}
The above if statement is displaying the 'found key' message when *pchar is equal to 0x42, 0x43, 0x47, 0x48, 0x4A.
Surely the message showed only be displayed when the pointer to the unsigned character is 0x42 (ASCII character 'B').
All of the above hex values have bit positon two set to '1' which seems to be influencing this although it shouldn't.
What could the problem be?