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
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
If you're dealing with shifts and masks, then you're dealing with binary. Do you know how binary relates to hexadecimal?
C Board - Homework Policy
What have you tried so far?
I tried this way.. I dont understand how to proceedCode: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); }
Do you understand why you get the output that you get from that code?
You're on the right track. As you move towards the MSB, the right shift will be a multiple of 4.
can anyone help in correcting me with the code. where to store the value after shifting and printing the right value at the output
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; }
Can anyone please help me with the program ?
I am confused .
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.
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 ?
So instead of printf'ing your character, put it somewhere safe.
after each shift and mask ..store the values in an array .. so if you get a decimal 10 .. use it as an Index intothen store that into another array .. arrayVal[10].Code:arrayHex[15] = {1,2,3,4...a,b,c ,d,e,f}
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
print this backwards .. starting from array 1 and 0 ..Code:arrayVal[0] = D arrayVal[1] = F
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 ,.
"I wish i could wish my wishs away"
"By indirections find directions out" -- William Shakespears
"Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"
Mizra -> love = Death...
RDB(Rocks yooo)..
http://www.cbeginnersunited.com
Are you ready for the Trix ???
No, I didnt understand what you said. Can you give me some little more push ??
nope ..i cant sorry
"I wish i could wish my wishs away"
"By indirections find directions out" -- William Shakespears
"Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"
Mizra -> love = Death...
RDB(Rocks yooo)..
http://www.cbeginnersunited.com
Are you ready for the Trix ???