Base conversion

Thread Solved

Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Base conversion

 
0
  #1
Oct 30th, 2006
I'm trying to find a way to convert a number in any given base to decimal and then to the target base. What i'm asking is how will I represent this procedure in the function for example 123 base 4 to decimal= 1x4^2+2x4^1+3x4^0=27. I was thinking of decrementing the raised power with a for statement.But i'm also using a for statement to access each number in the string array. heres my attempt

  1. #include<stdio.h>
  2.  
  3. int main(void)
  4.  
  5. {
  6. char num_string;
  7. int sourcebase;
  8. int targetbase;
  9. int temp
  10.  
  11. do {
  12. printf("Enter a positive number/n");
  13. scanf_s("%c",&num_string);
  14. printf("Enter the base of that number");
  15. scanf_s("%d", &sourcebase);
  16. printf("Enter the destination base");
  17. scanf_s("%d",&targetbase);
  18. if((sourcebase<2)||(sourcebase>10)){
  19. printf ("You number is invalid!!!!!!!/n/n");
  20. }
  21. }
  22. length= //some code to get string length
  23.  
  24. for(i=0;i<length,i++)
  25.  
  26. temp=num_string[i]*sourcebase //here should be corresponding index
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Base conversion

 
0
  #2
Oct 30th, 2006
You actually wanted num_string as char array( C style string) but have declared it as being char which will hold only one character.

  1. char num_string = '\0' ; // wrong
  2. char num_string[BUFSIZ] = {'\0'} ; // correct, will now hold a number string

Also can you give a sample output your program is supposed to display, since it would help in clearing hte problem stmt which is not so apparent to me, seeing your imcomplete implementation.
Last edited by ~s.o.s~; Oct 30th, 2006 at 1:49 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Base conversion

 
0
  #3
Oct 30th, 2006
Ok the program is supposed to convert to the target base of a number given the soucre base and the number. I am using decimal as a segway between bases after I get to decimal I'll divide out using the target base.

Enter a positive number : 120
Enter the base of that number (2 to 10) : 3
Enter the target base (2 to 10) : 5
The number in the target base is : 30
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Base conversion

 
0
  #4
Oct 30th, 2006
Okay I will give you the algorithm, you just try to implement it.
  • Accept the number, the source base and the target base from the user.
  • convert the given "number" having base "source_base" into decimal using the formula:
  1. while( tmp_number )
  2. {
  3. decimal += (tmp_number % 10) * pow( source_base, i ) ;
  4. tmp_number /= 10 ;
  5. ++i ;
  6. }
  • Now we have the decimal equivalent of the number in "decimal"
  • Create a dynamic array uisng pointer to int which will hold the individual digits of the new number of the target base.
  • Using the given formula convert from decimal to your base.
  1. while( decimal )
  2. {
  3. new_number[i] = decimal % target_base ;
  4. decimal /= target_base ;
  5. new_number = (int*) realloc( new_number, i + 2 ) ;
  6. ++i ;
  7. }
  • In the end you would get your required number by traversing through the integer array.
Hope it helped, bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Base conversion

 
0
  #5
Oct 30th, 2006
Does this negate the need for the for statement?How will it know which digit to multippy the base and exponent by?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Base conversion

 
0
  #6
Oct 30th, 2006
while constructs and for constructs are both looping constructs -- you can use either one of them for looping purpose if thats what you wanted to ask.

decimal += (tmp_number % 10) * pow( source_base, i ) ;

Here decimal is the var which will hold the decimal value of the entered number with the base "source_base". To it we iteratively add the digit * source_base ^ i.

Here digit is obtained by doing tmp_number % 10 while i controls the exponent value.
Last edited by ~s.o.s~; Oct 30th, 2006 at 3:43 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Base conversion

 
0
  #7
Oct 30th, 2006
ok (last question i swear )so when we say while(temp_number) when are we telling the loop to terminate? shouldn't we say
while(temp_number%10 !=0)?
Last edited by boujibabe; Oct 30th, 2006 at 4:00 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Base conversion

 
0
  #8
Oct 30th, 2006
while (temp_number) means continue with the loop while temp_number is not 0. Inside the loop I am dividing the temp_number with 10 (temp_number /= 10 ) so the loop ends when this value reaches 0.

eg.
Initial value = 129
129 / 10 = 12
12 / 10 = 1
2 / 10 = 0 ( loop ends )
Hope it made matters a bit clear for you.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Base conversion

 
0
  #9
Oct 30th, 2006
what is the realloc() doing here and why are we adding 2?I've never used it before
new_number = (int*) realloc( new_number, i + 2 ) ;
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Base conversion

 
0
  #10
Oct 30th, 2006
Hmmm... realloc is the memory management function which really makes the job of dynamic memory allocation actually "dynamic".

Dynamic memory when allocated using malloc, returns a pointer to the allocated peice of memory or memory block. But if the user wants to dynamically increase the size of the memory allocated to the same pointer, malloc wont do hte job.

To expand or to allocate more memory to the pointer which already has been allocated memory using malloc, we use realloc ( reallocation of memory ).

Consider this snippet:
  1. // result stored in the dynamically allocated integer array with
  2. // one slot
  3. int* new_number = (int*) calloc( sizeof( int ), sizeof( int ) ) ;
  4.  
  5. i = 0 ;
  6. while( decimal )
  7. {
  8. new_number[i] = decimal % target_base ;
  9. decimal /= target_base ;
  10.  
  11. new_number = (int*) realloc( new_number, i + 1 ) ;
  12. // we need more memory, which will be controlled by the counter i
  13. // hence while reallocing we use i as reference. So when i = 1
  14. // allocate (2 + 1) = 3 slots and so on.
  15.  
  16. ++i ;
  17. }

realloc takes two arguments, the first one being the pointer which has to be reallocated memory and the second one being the new size of the memory to be allocated.

For more info look here:
http://www.cplusplus.com/ref/cstdlib/realloc.html

Hope I have been helpful , bye.
Last edited by ~s.o.s~; Oct 30th, 2006 at 4:40 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC