Thread: Conversion of dec to hex

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    3

    Conversion of dec to hex

    This is my code the problem is when it prints a number the number appears very strange in the right bottom corner of a square with the 3 other corners being 0.My code is using a function's file(this one) a header file and a main.c file.I'm calling this function in the main.c file with the unsigned int a read from the keyboard.
    Code:
    #include<stdio.h>
    #include"L5P21.h"     //that's my header
    
    void conversion(unsigned int a,char c[])
    {
       unsigned int i=1,d,remainder,m,j;
       while(a!=0)
       {
             remainder=a%16;
             c[i]=remainder;
             m=10;
             for(d=0;d<5;d++)
             {
                 if(c[i]==m)
                    {
                           c[i]='A'+d;
                     }
                 m++;
              }
              i++;
    }
    printf("0X");
    for(j=i-1;j>0;j--)
    {
        printf("%c",c[j]);
    }
    }
    Last edited by Stephan-Alex; 04-15-2015 at 08:23 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It appears that if "a" is not zero initially, you have an infinite loop. The value of "a" controls the "while()" loop, but is never updated within that loop.

    I'd suggest you post the complete code (or at least a complete compilable example that illustrates the problem).

    Also, you should use more descriptive variable names so it's easier (for us and you) to read your code.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    3
    I've handled the trouble in the bold code now it works perfectly.
    Code:
    #include<stdio.h>
    #include"L5P21.h"     //that's my header
    
    void conversion(unsigned int a,char c[])
    {
       unsigned int i=1,d,remainder,m,j;
       while(a!=0)
       {
             remainder=a%16;
             c[i]=remainder;
             m=10;
             for(d=0;d<5;d++)
             {
                 if(c[i]==m)
                    {
                           c[i]='A'+d;
                     }
                 m++;
              }
              i++;
    }
    printf("0X");
    for(j=i-1;j>0;j--)
    {
        if(isalpha(c[j]))
        {printf("%c",c[j]);}
        else
           printf("%d",c[j]);
    }
    }
    [/QUOTE]

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    are you forbidden to use %x format for printing numbers?
    I cannot see a logic in the exercise that prohibits %x but allows %d
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by vart View Post
    are you forbidden to use %x format for printing numbers?
    I cannot see a logic in the exercise that prohibits %x but allows %d
    The logic behind the assignment could be that it is simply a learning exercise, intended to give the student practice and experience.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Matticus View Post
    The logic behind the assignment could be that it is simply a learning exercise, intended to give the student practice and experience.
    yeah, but to exercise it properly I would ban printf completely, allowing only puts
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with the conversion
    By erictu in forum C++ Programming
    Replies: 7
    Last Post: 03-22-2012, 01:13 PM
  2. How would you do this conversion?
    By redruby147 in forum C Programming
    Replies: 11
    Last Post: 04-04-2009, 02:38 PM
  3. C to C++ conversion
    By racerday182 in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2008, 06:20 PM
  4. .pdf to .txt conversion
    By cr_naik in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2003, 07:44 AM
  5. Conversion between C and C++
    By mcsd in forum C Programming
    Replies: 2
    Last Post: 08-13-2002, 07:39 PM

Tags for this Thread