Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Thread Tools Search this Thread Rate Thread
Old 07-May-2004, 17:33
machinated machinated is offline
Awaiting Email Confirmation
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about

Decimal to Hex conversion function

CPP / C++ / C Code:
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
		hdigits[0]='-';					//if _num is negative make the sign negative
		_num *= -1;						//and make _num positive to clear(zero) the sign bit
		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;

	return 0;


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 04:59
Strange behaviour in printf (interesting) nusstu C Programming Language 9 05-Apr-2004 13:36
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 20:46
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 09:02
i/o: set value x (long) before it is used by a function gmn C Programming Language 1 18-Nov-2003 02:12

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 21:55.

vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.