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

This is a discussion on C program to convert a decimal number to hex using masks and shifts within the C Programming forums, part of the General Programming Boards category; Hi, I am in dilemma. I couldn't figure out how to convert a decimal number to hex in C using ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're dealing with shifts and masks, then you're dealing with binary. Do you know how binary relates to hexadecimal?

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by whimsical1987 View Post
    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?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you understand why you get the output that you get from that code?

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    You're on the right track. As you move towards the MSB, the right shift will be a multiple of 4.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    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. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    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;
    }

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    Can anyone please help me with the program ?
    I am confused .

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    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 ?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So instead of printf'ing your character, put it somewhere safe.

  13. #13
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    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 ,.
    "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 ???

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    No, I didnt understand what you said. Can you give me some little more push ??

  15. #15
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    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 ???

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21