
07-May-2004, 18:33
|
|
Regular Member
|
|
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
|
|
|
Decimal to Hex conversion function
#include<iostream>
using namespace std;
/***************************************************************************************
Decimal to Hex converter
This is a template function which returns a char* array filled with hex digits when
passed to it a number(can work as a decimal to hex conversion)and will work for signed
and unsigned: char, short, and integer(long) type parameters passed to it.
Shortcomings:It won't work for double because of presence of bitshifting in its algorithm.
Other uses: you can pass it a hex number and it will return that char* array filled
with the original hex digits.
***************************************************************************************/
template <class type> //template usage to allow multiple types of parameters
char *type_char(type _num)
{
char *hdigits; //char array to store the result and return
unsigned char size = sizeof(type)*2;//2 hex digits for each byte(8bits)
hdigits = new char[size+2]; //first character for sign, last for null
char *hlookup = "0123456789abcdef"; //lookup table stores the hex digits into their
//corresponding index.
type temp = _num; //temporary place holder for _num of the same type
//as _num
if(_num<0)
{
hdigits[0]='-'; //if _num is negative make the sign negative
_num *= -1; //and make _num positive to clear(zero) the sign bit
}
else
hdigits[0]=' '; //else if _num is positive, make the sign blank(space)
char mask = 0x000f; //mask will clear(zero) out all the bits except lowest 4
//which represent a single hex digit
/*-----------------------------------------------------------------------------------------
The following for loop works this way: it starts with x as 0 and then right shifts
the temp number bits in multiples of x and 4. On the first pass, it won't shift(shift by 0)
because we need the lowest 4 bits for the lowest or right most hex digits. The next pass it
will right shift by 4(4*1), because we are done with the lowest 4 bits. Now we need the
4 bits higher than the lowest 4 bits, meaning bits 5-8 or 4-7 depending on how you count
from right. So we right shift by 4 bits pushing the bits 5-8(4-7) in to the lowest 4 bit
places, and then perform the AND operation on the number with our mask to clear(zero) all
of the bits except the lowest 4. Then the number that the lowest 4 bits represent, will be
used as an index to fetch a hex digit from the lookup array. The loop will run till x has
reached size(number of hex digit storage possible in the passed parameter type).
------------------------------------------------------------------------------------------*/
for(char x=0;x<size;x++)
{
temp = _num; //temp is assigned _num each time
temp >>=(4*x); //shift it in multiples of 4 bits each time
temp &= mask; //mask the integer so it will only have lowest 4 bits
hdigits[size-x]=hlookup[temp]; //temp now contains a numeric value which will point
//to the corresponding index in the look up table so
//all we have to do is use it as an index to the lookup
}
hdigits[size+1]= NULL; //the last element will store a null
return hdigits;
}
int main()
{
int _4byte;
cin>>_4byte;
cout<<endl<<type_char(_4byte)<<"H"<<endl;
return 0;
}
|
|