I want to make a number appear in the hexadecimal alphabet but I don't know how.
For example I used the number 255.I wanted the number to appear as "FF FF" but it appear as "15 15"
number is the number I want to convert
base is the desired alphabet (bin,dec,oct,hex)
str is the table with the digits
digits is how many digits I have
# include <iostream>
using namespace std;
class Number
{
private:
int number;
int base;
int str[80];
int digits;
void convert();
public:
Number(int n);
Number(int n,int b);
printNumber prints the number
void printNumber();
};
convert is the function that converts the number
void Number::convert()
{
int icount=0;
int x=number;
while(x!=0)
{
int m=x % base;
x=x/base;
str[icount]=m;
icount=icount+1;
}
digits count the number of digits I have
digits=icount;
}
this function uses as base dec
Number::Number(int n)
{
number=n;
base=10;
convert();
}
this function uses as base an integer b
Number::Number(int n,int b)
{
number=n;
base=b;
convert();
}
void Number::printNumber()
{
int i;
for(i=digits-1;i>=0;i--)
cout<<str[i]<<" ";
cout<<endl;
}
int main()
{
Number n1(255);
Number n2(254,16);
n1.printNumber();
n2.printNumber();
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}