Czech

g++

Converting hex to int. Converting hexadecimal class adress to a string and integer value.


07.03.26

Download Source (5.3kB)

1. Create simple class CHexToInt

#pragma once

#include <iostream>
#include <sstream>
using namespace std;

class CHexToInt
{
	string m_strId;			///< Class adress stored as string
	unsigned int m_iId;		///< Class adress stored as integer
    public:
        CHexToInt();
        ~CHexToInt();
};

2. In class constructor

CHexToInt::CHexToInt()
{
	stringstream stream;
	// Convert class adress to string
	stream << this;
	m_strId = stream.str();
	cout << "m_strId = " << m_strId << endl;
	// Convert class adress to integer
	m_iId = (int) this;
	cout << "m_iId = " << m_iId << endl;
	// Convert integer back to hexadecimal
	char strHexa[10];
	sprintf(strHexa, "%x", m_iId);
	cout << "Int to Hex = " << strHexa << endl;
	// Test Int to Hex by cout
	cout << "Hex number = " << hex << m_strId << endl << endl;
}

2.1. Convert class adress to a string m_strId

stringstream stream;
// Convert hexadecimal class adress to string
stream << this;
m_strId = stream.str();

2.2. Convert class adress to integer m_iId

m_iId = (int) this;

2.3 Check if int value is correct. Convert int to hex and compare with class adress.

// Convert integer back to hexadecimal
char strHexa[10];
sprintf(strHexa, "%x", m_iId);
cout << "Int to Hex = " << strHexa << endl;
// Test Int to Hex by cout
cout << "Hex number = " << hex << m_strId << endl << endl;




home / g++


Valid XHTML 1.0 Transitional