# Thread: C program to convert a decimal number to hex using masks and shifts

1. ## C program to convert a decimal number to hex using masks and shifts

Hi,

I am in dilemma. I couldn't figure out how to convert a decimal number to hex in C using shifts and masks. Can any one help as soon as possible. Please I need it urgent

2. If you're dealing with shifts and masks, then you're dealing with binary. Do you know how binary relates to hexadecimal?

3. Originally Posted by whimsical1987
Hi,

I am in dilemma. I couldn't figure out how to convert a decimal number to hex in C using shifts and masks. Can any one help as soon as possible. Please I need it urgent
C Board - Homework Policy

What have you tried so far?

4. Code:
```if ( number < 10 && number >= 0)
{
printf("%d", number);
exit(1);
}

for (i = 0; i < LENGTH_OF_SHORT; i+=4){
/* Obtain the least-significant bit of number */

digit = number & 15;

printf("%d",digit);

/* Right-shift the number 4 times to evaluate the next bit */
j = number >> 4;
printf("%d:\n",j);
}```
I tried this way.. I dont understand how to proceed

5. Do you understand why you get the output that you get from that code?

6. You're on the right track. As you move towards the MSB, the right shift will be a multiple of 4.

7. can anyone help in correcting me with the code. where to store the value after shifting and printing the right value at the output

8. this is my whole code.
Code:
```#include<stdio.h>
#include<stdlib.h>

/* Prototype functions */
void printHex(short);
char asciiDigit(int);

int main(void)
{
int num;
printf("Enter the number: ");
scanf("%d",&num);
printHex(num);

return 0;
}

void printHex(short number)
{
/* Shorts are two bytes on the x86 */
const char LENGTH_OF_SHORT = 16;

char digits[LENGTH_OF_SHORT];

int i;
int digit;
int j;

if ( number < 10 && number >= 0)
{
printf("%d", number);
exit(1);
}

for (i = 0; i < LENGTH_OF_SHORT; i+=4){
/* Obtain the least-significant bit of number */

digit = number & 15;

printf("%d",digit);

/* Right-shift the number 4 times to evaluate the next bit */
j = number >> 4;
printf("%d:\n",j);

/* Store the result in the array */
//digits[(LENGTH_OF_SHORT - 1) - i] = asciiDigit(digit);
}

/*for (i = 0; i < LENGTH_OF_SHORT; i++){
/* Print the digit */
//putchar(digits[i]);
// }
}

char asciiDigit(int digit)
{
int ascii = -1;
switch (digit)
{
case 0:
ascii = 48;
break;
case 1:
ascii = 49;
break;
case 2:
ascii = 50;
break;
case 3:
ascii = 51;
break;
case 4:
ascii = 52;
break;
case 5:
ascii = 53;
break;
case 6:
ascii = 54;
break;
case 7:
ascii = 55;
break;
case 8:
ascii = 56;
break;
case 9:
ascii = 57;
break;
case 10:
ascii = 65;
break;
case 11:
ascii = 66;
break;
case 12:
ascii = 67;
break;
case 13:
ascii = 68;
break;
case 14:
ascii = 69;
break;
case 15:
ascii = 70;
break;

}
return ascii;
}```

I am confused .

10. You can store things in variables. You should be able to count ahead of time how many variables you need, since there's a maximum size on an integer which corresponds to a maximum length in hex.

11. I use short integer which has to be converted to hexadecimal number.

When i use the & I am masking everything except the last 4 bits, which I'll be converting to the hex and then do the right shift 4 times which divides the number by 4.

This is my logic. If the logic is correct can you tell me which part of the code has to be edited ?

13. after each shift and mask ..store the values in an array .. so if you get a decimal 10 .. use it as an Index into
Code:
` arrayHex[15] = {1,2,3,4...a,b,c ,d,e,f}`
then store that into another array .. arrayVal[10].

once you have the values in arrayVal ... just print the values from the last point where you were in arrayVal ..

so if the values were FD
Code:
```arrayVal[0] = D
arrayVal[1] = F```
print this backwards .. starting from array 1 and 0 ..

you can do all of this with loops and counters ..the logic you are going is alright ..it should work that way .. i rememeber doing this in assembler like a month ago .. if you still dont understand ..whats going on ..let me know .. i could spend some time ..looking at your code and perhapd fiddling with it ,.

14. No, I didnt understand what you said. Can you give me some little more push ??

15. nope ..i cant sorry