We help IT Professionals succeed at work.

conversion of 32-Bit unsigned int to decimal value

sniper4897
sniper4897 asked
on
697 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);
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006

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

Author

Commented:
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
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

Commented:
I think i may have set this up wrong because it's not giving me the decimal value
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

Commented:
I set up this loop for (i=0; i%10; i++)...but it doesn't seem to run
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

Commented:
Sry this is my first time programming...thanks for your help...I'll try and get another book on it
CERTIFIED EXPERT
Top Expert 2006

Commented:
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*/
}

Author

Commented:
i would use that but i have to use a for loop
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Commented:
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

Author

Commented:
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);
CERTIFIED EXPERT
Top Expert 2006

Commented:
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

Author

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

Commented:
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
Top Expert 2004

Commented:
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
Top Expert 2004

Commented:
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.
CERTIFIED EXPERT
Top Expert 2006

Commented:
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
Top Expert 2004

Commented:
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
CERTIFIED EXPERT
Top Expert 2006

Commented:
I do not think shifts in C are circular
Top Expert 2004

Commented:
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
Top Expert 2004

Commented:
...which is exactly what you did. Ouch. You're right.

Author

Commented:
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); ?
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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);



CERTIFIED EXPERT
Top Expert 2006
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
ok..thanks..i'll just put it in a seperate loop
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.