Solved

conversion of 32-Bit unsigned int to decimal value

Posted on 2004-04-12
28
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 9
  • 5
  • +4
28 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10810435
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
ID: 10810439
char * num = new char (10);
gets (num);
for (;*num;num++)
printf ("\n%c", *num);
0
 

Author Comment

by:sniper4897
ID: 10810446
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 10810464
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
ID: 10810491
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
ID: 10810505
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
ID: 10810543
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
ID: 10810572
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
ID: 10810642
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
ID: 10810653
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
ID: 10810657
i would use that but i have to use a for loop
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10810663
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
ID: 10810679
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
ID: 10810680
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10810692
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
ID: 10810726
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
ID: 10811032
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
ID: 10812436
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
ID: 10812477
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
ID: 10812602
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
ID: 10812878
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
ID: 10812970
I do not think shifts in C are circular
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10813853
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
ID: 10813867
...which is exactly what you did. Ouch. You're right.
0
 

Author Comment

by:sniper4897
ID: 10815671
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:Kent Olsen
ID: 10819410
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
ID: 10820434
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
ID: 10826103
ok..thanks..i'll just put it in a seperate loop
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.

734 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