Cracking the Coding Interview: Fourth Edition (308 page e-book / PDF)
Delivered instantly as a PDF via email. For Software Engineers & SDETs
  • 150 programming interview questions and answers
  • 5 proven approaches to crack algorithm questions
  • 10 mistakes candidates make, and how to avoid them.
  • How to prepare for technical and behavioral questions without wasting your time!
"The BEST book for acing your interview. It helped me land my Microsoft job, and it was worth every penny!" - Ravi (Accepted at Microsoft, Amazon and Facebook) 30 Day Money Back Guarantee: Don't love the book? We'll give you your money back! More Info

Saturday, April 24, 2010

Convert a decimal number

How do you convert a decimal number to its hexa-decimal equivalent. Give a C code to do so.

5 comments:

  1. Program to convert input decimal value to its hexadecimal equivalent

    #include
    #include
    #include
    void dtoh(int d);
    main()
    {

    int d;
    clrscr();
    printf("Enter a no. in decimal system:- ");
    scanf("%d",&d);
    dtoh(d);
    printf("




    HAVE A NICE DAY! BYE.");
    getch();
    }

    void dtoh(int d)
    {
    int b,c=0,a[5],i=0;
    b=d;
    while (b>15)
    {
    a[i]=b%16;
    b=b/16;
    i++;
    c++;
    }
    a[i]=b;
    printf("
    Its hexadecimal equivalent is ");
    for (i=c;i>=0;--i)
    {
    if (a[i]==10)
    printf("A");
    else if (a[i]==11)
    printf("B");
    else if (a[i]==12)
    printf("C");
    else if (a[i]==13)
    printf("D");
    else if (a[i]==14)
    printf("E");
    else if (a[i]==15)
    printf("F");
    else
    printf("%d",a[i]);
    }
    return;
    }

    ReplyDelete
  2. int i=36;
    printf("hexa : %0x",i);

    ReplyDelete
  3. char hexRep[16] = {'0', '1', '2', '3', '4', '5', '6','7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

    char
    hexChar(int digitValue)
    {
    return hexRep[digitValue];
    }

    void
    swapChars(char& a, char& b)
    {
    char temp = a;

    a = b;
    b = temp;
    }

    void
    reverseStr(char inOut[], int length)
    {
    int start = 0, end = length - 1;

    while ( start < end){
    swapChars(inOut[start], inOut[end]);
    start++; end--;
    }
    }

    void
    convertHex(int decimalIn)
    {
    int value = decimalIn;
    int reminder = 0;
    int It = 0;
    int length = 0;
    char *out = new char[128];

    memset(out, '\0', 128);


    while(value){

    if (value < 16){
    out[It++] = hexChar(value);
    value = 0;
    out[It++] = 'x';
    out[It++] = '0';
    length = It;
    out[It++] = '\0';

    }
    else{
    reminder = value % 16;
    value = value / 16;
    out[It++] = hexChar(reminder);
    }
    }

    reverseStr(out, length);
    cout << out;
    delete [] out;
    };


    int _tmain(int argc, _TCHAR* argv[])
    {
    int decimalIn = 128;
    convertHex(decimalIn);
    }

    ReplyDelete
  4. /* used recursion to avoid doing a reverse! */
    int hex(int i)
    {
    if (!i) return;
    hex(i/16);
    int d = i%16;
    printf("%c", (d<10?d+'0':d-10+'a'));
    return;
    }
    /* wrapper for hex! */
    int dectohex (int i)
    {
    if (i==0) {
    printf("%d = 0x0\n", i);
    return;
    }
    printf ("%d = 0x", i);
    hex (i);
    printf ("\n");
    return;
    }

    /* testing */
    int main()
    {
    dectohex(16);
    dectohex (65535);
    return 0;
    }

    ReplyDelete
  5. #include
    #include
    #include

    int decimal2hex(unsigned int n/*decimal_num*/, char buf[32]){

    char digitlookup[] ="0123456789ABCDEF";

    unsigned int k=1;
    int i = 30;
    buf[31] =0;
    while(n){
    k = n & 0xF;
    buf[i--] = digitlookup[k];
    n = n >>4;
    }
    return;
    }

    int main(){

    unsigned int i = 0xfa12cd;
    char buf[32] ={0};
    memset(buf, ' ', 32);
    printf("Enter decimal number is:%d (%X).\n", i, i);

    decimal2hex(i, buf);
    printf("Converted number in hex:%s\n", buf);

    return 0;
    }

    ReplyDelete