Memory Print of an integer in C, C++
Here is a small program to demo the memory print of an integer value :
If you have any suggestion let me know in comments.
Thanks for visiting.
#include <iostream>
#include <conio.h>
#include <bitset>
using namespace std;
int GetDecimalNumber(char *data)
{
int len = strlen(data) - 1;
int result = 0;
while(len >= 0)
{
if (*data == '1')
result += pow(2.0, len);
++data;
--len;
}
return result;
}
string binaryReprentation(int num)
{
return bitset<CHAR_BIT>(num).to_string();
}
void main()
{
char *data = "00010000000000001101110101101101";
int num = GetDecimalNumber(data);
int *numPtr = #
char *ptr = reinterpret_cast<char *>(numPtr);
cout << "Actual Data : " << data << endl;
cout << "Memory Print : ";
for (int index = 0; index < sizeof(num); ++index)
{
unsigned char byte = *(ptr + index);
cout << binaryReprentation((unsigned)byte);
}
_getch();
}
If you have any suggestion let me know in comments.
Thanks for visiting.
Comments
Post a Comment