decimal 2 octa and hex

kindly give   me some algorithm for converting a decimal number into octal and hex.is is possible that all the number conversions like decimal 2 octal,binary and  hex can be done by some common logic?
shilpi84Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nabehsCommented:
try:
http://www.codeproject.com/cpp/baseconvert.asp

To convert between base 10 to base 8 (octal) for example:

TCHAR szNumber[24];
long nNum = 105; // for example convert 105 decimal to octal
NumToStr(szNumber, nNum, 10);

// convert to octal
int nOctalNum;
StrToNum(szNumber, lstrlen(szNumber), 8);
nabehsCommented:
also check out functions _itoa

Jaime OlivaresSoftware ArchitectCommented:
sscanf and sprintf with proper formatting character will help also
%i for decimal
%o for octal
%x for hexa

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

shilpi84Author Commented:
sorry i 4got 2 mention that i m not supposed 2 use itoa and sprintf functions i have 2 make my own function.
shilpi84Author Commented:
nabehs i didnt understand any thing in the link given by u.also i dont have 2 take the number as a string.
shilpi84Author Commented:
>>i 4got 2 mention that i m not supposed 2 use itoa and sprintf functions i have 2 make my own
>>function.
i thought i was bng specific .and also i  specified  that i needed an algorithm not the c code.i also searched for the solutions 2 the number conversions their the experts have provided the c codes what guarentee is it that those questions werent homeworrks?also if i would have just wanted the c code i would have taken from those solutions i came across while searching.but i really want 2 understand.
waysideCommented:
To convert from decimal to octal or hexidecimal (or any power-of-2 based radix) is easy because the numbers are already stored by the computer in base 2 (binary).

Consider a 32 bit number in binary

10010100101011010100101010010101

To convert to octal, simply group this binary number into groups of 3 bits, starting from the right:

10 010 100 101 011 010 100 101 010 010 101

Then convert each group of 3 bits into a number, eg, 101 = 5

For hexidecimal, use groups of 4:

1001 0100 1010 1101 0100 1010 1001 0101

Now convert each group of 4 bits into the correct digit. for example, 1010 = a

To do this grouping you want to use the bit-mask operator (&) and bit-shift operator (>>). There are several ways you could do it, such as extracting one bit at a time and calculating the total for each digit, or masking off 3 or 4 digits at a time, and then shifting the bits to move the next set into the lowest bits.

For example, to extract out the lowest 3 bits, do something like  

int num = 654375;
int lowest3bits = num & 0x00000007; // 7 is 111 in binary, that is, 3 bits are set

to get the next set of bits, shift them down:

num = num >> 3;

>  is possible that all the number conversions like decimal 2 octal,binary and  hex can be done by some common logic?

Sure, have your code use different masks and shifts based on which power-of-2 radix you want.

Note that using bit-masking and shifting only work for power-of-2 radices like 2, 8, and 16. For a more generic solution using any radix (including 2, 8, and 16) you'll want to explore the integer division and modulus operators (/ and %).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shilpi84Author Commented:
thanks  wayside:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.