Solved

conversion of 32-Bit unsigned int to decimal value

Posted on 2004-04-12
28
562 Views
Last Modified: 2010-04-15
I need to write a program to print out the decimal value of a 32-bit unsigned int (the value input by the user) digit by digit only using a for loop and the statement for output a single character: printf("%c", ch);
0
Comment
Question by:sniper4897
  • 10
  • 9
  • 5
  • +4
28 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Is this homework ? It sure sounds like one!

Experts here cannot do homework for you ... Show us what you have done till now and we will try to get you to a solution
0
 
LVL 4

Expert Comment

by:bkfirebird
Comment Utility
char * num = new char (10);
gets (num);
for (;*num;num++)
printf ("\n%c", *num);
0
 

Author Comment

by:sniper4897
Comment Utility
unsigned int n;
      unsigned int Power;
      int i;
      char ch;

      printf("please input an integer to convert: ");
      fflush(stdin);
      scanf("%d", &n);
      Power = 0x80000000;
      for (i=0; i<32; i++)
      {
            ch = (n & Power)? 1:0;
            printf("%c\n", ch);
            Power >>= 1;
      }
THis is what i have...i can't figure out what i am doing wrong
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
This is not decimal value but binary value that you are printing

>ch = (n & Power)? 1:0;
should be
ch = (n & Power)? '1':'0';
you are assigning ASCII 1 and 0 to ch while you need character 1 and 0
0
 

Author Comment

by:sniper4897
Comment Utility
I think i may have set this up wrong because it's not giving me the decimal value
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
you are printing binary value here ...

If you want decimal value, then you need to determine each digit ...
something like in a loop, get %10 .. this is the units digit ... store it and divide the number by 10 to get rid of the digit we already have

A simpler way would be to use sprintf
0
 

Author Comment

by:sniper4897
Comment Utility
I set up this loop for (i=0; i%10; i++)...but it doesn't seem to run
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
because you have not specified any condition for termination in the loop ... what I meant was

e.g. you have 45,

get 45 % 10   ... this equals 5, save it
divide 45 by 10 ... this equals 4
get 4%10 .. this equals 4, save it
divide 4 by 10 ... this equals 0 .... this is your loop termination condition ... no more input to process

I am not specifying the loop explicitly .... Think a bit ;o) ... Its not that difficult
0
 

Author Comment

by:sniper4897
Comment Utility
Sry this is my first time programming...thanks for your help...I'll try and get another book on it
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
Ok ... here is a hint .... while loop seems to fit this case better than for loop ...

while ( we have something to process )
{
  /* the procesing goes here*/
}
0
 

Author Comment

by:sniper4897
Comment Utility
i would use that but i have to use a for loop
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
You can fit the same thing into the for loop .... Its just thinking this in while loop that is easier

while ( i < 10 ) is same as for (;i<10;)  .... initialization and increments in for loop are not mandatory ... they can be left out
0
 
LVL 8

Expert Comment

by:akshayxx
Comment Utility
if u still want to go with for loop,  any while loop can be represented with for loop and vice-versa

while(isTrue(some_condition)){

}
can be changed to for loop as

for(;isTrue(some_condition);){

}
it doesnt matter much what loop you use, you must concentrate on algorithm. its just that in for loop  initialisations and loop-steps more visible.

hope u understood the algo sunny already explained..any further queries?
good luck
akshay
0
 

Author Comment

by:sniper4897
Comment Utility
ok this is what i have...am i even on the right track?       
printf("please input an integer to convert: ");
      fflush(stdin);
      scanf("%d", &n);
      Power = 0x80000000;

      {
            for (i=n; i%10; i++)
            {
                  
      ch = (n & Power)? '1':'0';
      Power >>= '1';
      n= i/10;
      printf("%c", ch);
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
You need to change the entire processing .... the contents of the loop are the same as the ones which gave you binary equivalent while you wanted decimal


     Power = 0x80000000;
     for (i=0; i<32; i++)
     {
          ch = (n & Power)? '1':'0';
          printf("%c\n", ch);
          Power >>= 1;
     }
will give you the binary string equivalent


to get the decimal refer to my post above
http:#10810505 and http:#10810572
0
 

Author Comment

by:sniper4897
Comment Utility
i guess i just don't get the algo...thanks for your help...if i need help wit another question i'll come back

0
 
LVL 8

Expert Comment

by:ssnkumar
Comment Utility
Hi sniper4897,

Looking at the conversation going on, I think the question is still not clear!
Why don't you take up one small example and explain?
Tell us what is the input you are going to give?
Then tell us, what is the output that you want to see on the console?

With this we must be able to understand what you are trying to do and then analyze your code....

-ssnkumar
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
Hi sniper4897,
printf("please input an integer to convert: ");
     fflush(stdin);
     scanf("%d", &n);
     Power = 0x80000000;

     {
          for (i=n; i%10; i++)
          {
               
     ch = (n & Power)? '1':'0';
     Power >>= '1';
     n= i/10;
     printf("%c", ch);

You have a couple of problems here. First of all, you're working in binary mode, not decimal.

Here is a small example for character-by-character conversion in decimal:

void int_out(unsigned int inp){
    while(inp){
        putchar('0' + inp % 10);
        inp /=10;
    }
}

This will only serve as a small hint, though. It won't handle zero as input, and the output order is wrong. The zero problem is simple - I'm sure you can solve it in almost no time. The output order problem is slightly more tricky.

You already had a basic idea how to handle output in the proper left-right direction using binary. Now you should step back a bit and think how you can do the same thing with decimal.

Cheers,
Stefan
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
BTW: The shift command you used,
Power >>= 1;
is shifting in the wrong direction.

And using
Power >>= '1';
is identical to
Power >>= 48;
as the ascii code for '1' is 48. This is for sure not what you wanted.
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
BTW: The shift command you used,
Power >>= 1;
is shifting in the wrong direction

I think this is correct ... He prints from left to right
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
sunnycoder,
As the MSB is checked by the mask first, it should be the 2nd bit from the left checked next, so a left shift should be fine.

Stefan
0
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
I do not think shifts in C are circular
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
sunnycoder,
No, this is not what I mean. You want to print the MSB first by masking it with 0x8...0. For the following bits, you can either shift the mask to the right, or your integer to the left.

Stefan
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
...which is exactly what you did. Ouch. You're right.
0
 

Author Comment

by:sniper4897
Comment Utility
I think i may have gotten it......
{
int i;
      unsigned int n;
      unsigned int Power;
      char ch;
      printf("please input an integer to convert: ");
     fflush(stdin);
     scanf("%d", &n);
     Power = 0x80000000;

          for (i=n; i%10; i--)
              {
               
     ch = (n & Power)? '1':'0';
     Power >>= '1';

{
    while(n>0)
      {
        putchar('0' + n % 10);
            printf("\n");
        n /=10;
      }
}
              }
Is the putchar('0"+ n%10); statement the same as Printf("%c", ch); ?
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi sniper,

The putchar() and printf() are very close in functionality.  Since your rules state that you must use printf(), just substitute the value calculated with the putchar() into the printf().

printf ("%c", '0' + n%10);



0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
Comment Utility
well done .... But why are you using nested loops !!!

          for (i=n; i%10; i--)
          {              
                ch = (n & Power)? '1':'0';
                Power >>= '1';           ======== If you do not need binary string equivalent, then you can get rid of all these statements except the while loop .... If you do need this conversion then keep them in separate loops

                {
                while(n>0)
                {
                         putchar('0' + n % 10);
                         printf("\n");
                         n /=10;
                }
                }
         }
===================================
this should be sufficient
     int i;
     unsigned int n;
     unsigned int Power;
     char ch;
     printf("please input an integer to convert: ");
     fflush(stdin);
     scanf("%d", &n);
     while(n>0)
     {
        putchar('0' + n % 10);
          printf("\n");
        n /=10;
     }
0
 

Author Comment

by:sniper4897
Comment Utility
ok..thanks..i'll just put it in a seperate loop
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now