C++: Addressing each byte - c++

Trying to extract each byte in a short I've created.
While I can print the second (or first) byte, I can't get both.
With my current understanding, this should work. Hoping someone can make it clear to me what the error is here. I'm running this on windows x86, so ILP32 data format.
#include <iostream>
using namespace std;
int main()
{
short i = 0x1123;
short* p = &i;
short* p1 = p;
p1++;
char c = *p;
char c1 = *p1;
cout
<< "Short: " << hex << i << '\n'
<< "p: " << p << '\n'
<< "p1: " << p1 << '\n'
<< "c: " << hex << (unsigned int)c << '\n'
<< "c1: " << hex << (unsigned int)c1 << '\n'
;
return 0;
}
Output:
Short: 1123
p: 0041FB58
p1: 0041FB56
c: 23
c1: ffffffcc

Here is the fix:
#include <iostream>
#include <iomanip>
int main()
{
short i = 0x1123;
short* p = &i;
char* c = reinterpret_cast<char*>( p );
char* c1 = c + 1;
std::cout << "Short: " << std::showbase << std::hex << i << '\n'
<< "p: " << p << '\n'
<< "c: " << +( *c ) << '\n'
<< "c1: " << +( *c1 ) << '\n';
return 0;
}
There is no need for p1. The problem with p1++ is that it adds 2 to the value of p1 and not 1 as you would expect. And that's because p1 is a pointer to short so when you increment it, it progresses by 2 (short is 2 bytes). You need to cast it to a char* so that each time you increment it, it will add 1 to the value.
Also, notice the + operator in +( *c ). Using this operator will make sure that *c won't be printed as a character but instead as its ASCII value or whatever value it has, thus there is no need to cast it to int.
Possible output:
Short: 0x1123
p: 0xb71f3ffd8e
c: 0x23
c1: 0x11

An alternative solution that uses no pointer trix and doesn't care about endianness.
#include <iostream>
int main()
{
short i = 0x1123;
unsigned low = i % 256;
unsigned high = i / 256;
std::cout << std::hex
<< "Short: " << i << '\n'
<< "low: " << low << '\n'
<< "high: " << high << '\n';
return 0;
}

Thank you to rustyx and Pete Becker who answered this. The program is aware that the pointer is for a type short therefore when I try to increment it automatically increments the value by 2. This behaviour can be circumvented by casting the pointer to a char.
#include <iostream>
using namespace std;
int main()
{
short i = 0x1123;
cout
<< "Short: " << hex << i << '\n'
;
char* p = (char*)&i;
char c = *p;
cout
<< "p: " << hex << (unsigned int)p << '\n'
<< "c: " << hex << (unsigned int)c << '\n'
;
char* p1 = p + 1;
char c1 = *p1;
cout
<< "p1: " << hex << (unsigned int)p1 << '\n'
<< "c1: " << hex << (unsigned int)c1 << '\n'
;
return 0;
}
Output:
Short: 1123
p: 54fd54
c: 23
p1: 54fd55
c1: 11

Related

C++ pointer referencing itself not producing expected output

I'm working with C++ pointers and I've encountered something curious.
If I reset a pointer to itself using "b = (int*)&b;", I expected the deferenced output to be the memory address of itself -- since it was pointing to itself.
So I thought *b would be "0x7ffea00819b0", but it's some strange numeric value.
But this isn't the case. The alternate value I get is confusing.
Here is my output:
Value of a = 10
Address of a = 0x7ffea00819ac
Value of b = 0x7ffea00819b0
Address of b = 0x7ffea00819b0
Dereference of b = -1610081872
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a = 10;
int *b = &a;
b = (int*)&b;
cout << "Value of a = " << a << endl;
cout << "Address of a = " << &a << endl << endl;
cout << "Value of b = " << b << endl;
cout << "Address of b = " << &b << endl;
cout << "Dereference of b = " << *b << endl;
}
Changing the type to unsigned long int fixed the issue.
Thank you all!
#include <iostream>
#include <type_traits>
using namespace std;
int main(int argc, char** argv)
{
unsigned long int a = 10;
unsigned long int *b = &a;
b = (unsigned long int*)&b;
cout << "Value of a = " << a << endl;
cout << "Address of a = " << &a << endl << endl;
cout << sizeof b << endl;
cout << sizeof *b << endl;
//static_assert(sizeof b == sizeof *b);
cout << "Value of b = " << b << endl;
cout << "Address of b = " << &b << endl;
cout << "Dereference of b = " << hex << "0x" << *b << endl;
}

Printing unsigned char(BYTE) in Hex format in c++. Using std::cout

I've looked through all previous version of this topic and none have seemed to help.
I've got a value located in unsigned char called Offset.
When printing this with printf("%02X\n", Offset); It prints out the correct value which is 0x2B.
Though when I try with std::cout << std::hex << Offset << std::endl; It always prints a different value.
I have also tried:
std::cout << std::hex << (unsigned int)Offset << std::endl;
std::cout << std::hex << static_cast<unsigned>(Offset) << std::endl;
Though none have seemed to print out the correct value.                    
As you defined "Offset" like this unsigned char Offset = 0x2B;, Offset's type now is unsigned char and you cant not print a unsigned char variable as a std::hex.
You must first convert it to the std::hex type then print it.
#include <iostream >
int main() {
unsigned char Offset = 0x2B;
printf("%02X\n", Offset);
std::cout << std::hex << Offset << std::endl;
std::cout << std::hex << (unsigned int)Offset << std::endl;
std::cout << std::hex << static_cast<unsigned>(Offset) << std::endl;
return 0;
}

simple game, while loop and checking strings

I am a beginner at c++ and I want to create simple game. You have vector of strings, then you check if line input matched the right answer.
I want to generate random number 1 ,2 or 3. Then check if line matches correct answer and count the points.
I am probably missing something basic, yet I dont know what.
Problems:
Input line get correctly read on only first iterations
somehow points (tocke) jumps to 45763 after finishing.
At beginning time (cas) is sometimes 2.
Code:
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <string>
int main() {
int runde;
int tocke;
int cas;
std::cout << "\n" << "Pravila igre:" << "\n" << "Za pravilen odgovor dobis 1 tocko, za napacnega zgubis 2!"<<
"\n" << "Stevilo zivljenj si izberes sama!"<< "\n" << "\n" ;
std::cout << "Izberi stevilo zivljenj!:" << "\n";
std::cin >> runde ;
std::vector<std::string> latin = {"carum carvi", "artemisia absiinthium","coriandrum sativum"};
std::vector<std::string> slovene = {"navadna kumina", "pravi pelin", "koriander"};
tocke << 0;
cas << 0;
do {
int ind;
cas << cas + 1;
std::cout << "Round " << cas <<"! Ladies and gentlemans, buckle your seatbelts!"<<"\n" << "\n" ;
ind = std::rand() % 3;
std::cout << "ime rastline: " << slovene[ind] << "\n";
std::cin.ignore();
std::string line;
getline(std::cin, line);
std::cout << "\n";
if (latin[ind] == line){
std::cout << "Pravlino! Tocka zate!" << "\n";
tocke << tocke + 1;
std::cout << "Tocke == " << tocke << "\n" << "Zivjenja == " << runde << "\n" << "Prezivete runde == " << cas << "\n"<< "\n";
}
else
{
std::cout << "Napaka! :D" << "\n";
std::cout << "Pravilen odgovor == " << latin[ind] << "\n";
-- runde ;
tocke << tocke - 2;
std::cout << "Tocke == " << tocke << "\n" << "Zivjenja == " << runde << "\n" << "Prezivete runde == " << cas << "\n"<< "\n";
}
}while(runde >= 0 );
std::cout << "\n"<<"Stevilo tock == " << tocke <<"\n" << "St. prezivetih rund == " << cas - 1
<< "\n" ;
}
You seem to have a misconception regarding operators. << is NOT assignment, use = instead. So tocke << 0; doesn't assign 0 to tocke, it does bitshifting (on an uninitialized variable), then discards the result. tocke stays uninitialized and this causes problems later.
Instead of this:
tocke << 0;
cas << 0;
Do this:
tocke = 0;
cas = 0;
Also instead of cas << cas + 1; do cas++ and instead of tocke << tocke - 2; do tocke -= 2;. To learn how the assignment operators work, you can read about them here. Last but not least, try to see if your compiler gives you any warnings, it should complain about using uninitialized values.

Converting fl in hexadecimal in c++

I am new to C++, and programming, and I want to write a C++ program to convert a float in hexadecimal with the help of pointers
I've looked on other threads and really tried to get a hold of this but can't seem to figure it out.
Here is what I have done so far:
int main()
{
float number = -12.0
unsigned char *ptr = ((unsigned char*) &number);
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte " [i] << "is : " << ptr[i] << endl;
}
So with this, i assume I am able to have access to the bytes that compose the float. However, can you suggest any ideas to convert this to hexadecimal. I guess, I have to be able to read the binary behind all this... but I am not really sure how.
Note: I understand how to convert from binary to hexadecimal
iostreams can print numbers in hexadecimal:
int main() {
float number=-1.0;
unsigned char *ptr = ((unsigned char*)&number);
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte ";
cout << setw(0) << dec << i << "is : ";
cout << setw(2) << setfill('0') << hex << ptr[i] << endl;
}
what you want might be sth like this:
float number=-1.0;
char cz[]="0123456789ABCDEF";
int si=sizeof(float);
char* ptr=(char*)(&number);
for(int i=0;si-i;++i){
std::cout<<cz[((*ptr)&0xf0)>>4]<<cz[(*ptr)&0x0f]<<' ';
++ptr;
}
You have minor bug here cout << "Byte " [i] which is the same as doing:
const char *text = "Byte ";
cout << text[i]
Another minor bug is that ptr[] is char thus cout treats it as a printable character (not a number), using (int)ptr[i] will do. Also you want print i as decimal and ptr[i] as hex, so you have to use std::dec and std::hex
cout << "Byte " << std::dec << i << " is : " << std::hex << (int)ptr[i] << endl;
You may polish it a bit by using std::setw() and std::setfill() from <iomanip>:
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main()
{
float number = -1.0;
unsigned char *ptr = ((unsigned char*) &number);
cout << std::setfill('0');
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte "
<< std::dec << std::setw(0) << i << " is : "
<< std::hex << std::setw(2) << (int)ptr[i] << endl;
}
return 0;
}
This will print out:
Byte 0 is : 00
Byte 1 is : 00
Byte 2 is : 80
Byte 3 is : bf

C++ strcpy pointers that are passed to a function

I am passing an string or a char array to a function and swapping them but losing the first char array's value for some reason. Here is my code:
void foo(char* a, char* b){
char* temp;
temp = new char[strlen(a)+1];
strcpy(temp, a);
strcpy(a, b);
strcpy(b, temp);
delete[] temp;
}
So in foo the function gets passed two pointers and the are attempted to be swapped.
Here is the main function. There may be a problem with the passing of the variable, but the compiler did not give me an issue.
int main(){
char a[] = "First";
char b[] = "Last";
std::cout << "A Before: "<< a << "\n";
std::cout << "B Before: " << b << "\n\n";
foo(a, b);
std::cout << "A After: "<< a << "\n";
std::cout << "B After: "<< b << "\n\n";
return 0;
}
The output I am getting is as follows:
A Before: first
B Before: last
A After:
B After: first
Now I have tested the values of the strings while in the function during the strcpy's and turns empty after the final strcpy, which means, or at lest I think, that the problem lies within the pointers to the original variables. It could be a chain reaction type of thing where all of the pointers are pointing to the "a" and it confuses the program.
Any help would be appreciated, also why this is happening would be very useful as well.
Because your string a is longer than b.So strcpy does not work as you expect in line:
strcpy(b, temp);
Tips:
Use strncpy instead of strcpy
Use c++ Strings instead of the c style string.Then you can swap
them with a.swap(b);
The problem is your array sizes happen to be such that you are clobbering your stack; fortunately, for you, the effect is simply to place a null byte in the first character of a, making it an empty string.
#include <iostream>
#include <string.h>
void foo(char* a, char* b){
char* temp = new char[strlen(a)+1];
strcpy(temp, a);
std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
strcpy(a, b);
std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
strcpy(b, temp); // this copies 6 bytes to b, which puts a 0 in the first byte of a.
std::cout << "temp = " << temp << " a = " << a << " b = " << b << std::endl;
delete[] temp;
}
int main() {
char a[] = "First";
char b[] = "Last";
std::cout << "a size is " << sizeof(a) << std::endl;
std::cout << "b size is " << sizeof(b) << std::endl;
std::cout << "address of a[0] is " << (void*)&a[0] << std::endl;
std::cout << "address of b[0] is " << (void*)&b[0] << std::endl;
foo(a, b);
std::cout << "A After: "<< a << "\n";
std::cout << "B After: "<< b << "\n\n";
}
http://ideone.com/fDvnnH
a size is 6
b size is 5
address of a[0] is 0xbfec5caa
address of b[0] is 0xbfec5ca5
temp = First a = First b = Last
temp = First a = Last b = Last
temp = First a = b = First
A After:
B After: First
You might want to investigate std::string or look at using std::strncpy