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

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?

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

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.

%i for decimal

%o for octal

%x for hexa

>>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.

Consider a 32 bit number in binary

10010100101011010100101010

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
System Programming

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get this solution by purchasing an Individual license!
Start your 7-day free trial.

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);