### Thread: How to convert form base 10 to base 6

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
22
Rep Power
0

#### How to convert form base 10 to base 6

hello

i need yours help,i try to convert form base 10 to base 6

i know how to do it to base 4 ,but i cant fix my code for base 6

this is my code

PHP Code:
``` #include "utils.h" #include <stdlib.h> /**  * Convert a decimal number to a ten bits string.  *  * @param decimal  *   The number to convert.  * @param buffer  *   A buffer to save the string to.  */ void base4code(long decimal, char* buffer) {     int i;          for (i = CODE_SIZE - 1; i >= 0; i--) {         buffer[i] = '0' + (decimal & 1) ;         decimal = decimal >> 1;         buffer[i] = buffer[i] + 2 * (decimal & 1);         decimal = decimal >> 1 ;     }     buffer[CODE_SIZE] = '\0'; } /**  * Convert decimal number to base 4.  *  * @param decimal  *   The number to convert.  *  * @return  *   The number converted to base 4.  */ int base4(int decimal) {     int digit, scale = 1, result = 0;     decimal = abs(decimal);     while(decimal > 0) {         digit = decimal % 4;         result += digit * scale;         decimal /= 4;         scale *= 10;     }     return result; }  ```
2. convert_to_base_6_verb_in_j =: 6&(#.inv)

If you had written a general purpose "convert to base 4" program this problem would be solved by changing the 4 to a 6.
Last edited by b49P23TIvg; November 8th, 2013 at 10:16 AM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
22
Rep Power
0
so I need to chance this

PHP Code:
``` int base4(int decimal) {      int digit, scale = 1, result = 0;      decimal = abs(decimal);      while(decimal > 0) {          digit = decimal % 4;          result += digit * scale;          decimal /= 4;          scale *= 10;      }  ```
to this
PHP Code:
``` int base6(int decimal) {      int digit, scale = 1, result = 0;      decimal = abs(decimal);      while(decimal > 0) {          digit = decimal % 6;          result += digit * scale;          decimal /= 6;          scale *= 10;      }  ```
4. What are you trying to do? Here is much more efficient code for what your code is trying to do:
Code:
```int base4(int decimal)
{
int result = decimal;

return result;
}```
Just like your code is trying to do, it takes decimal, which is a binary value, copies it to result, which is another binary value, and returns that binary value.

Your code does absolutely nothing towards converting from one number base to another number base, because you're just going from binary to binary. You are forgetting that one basic fact about digital computers: In general, in a digital computer, all data is stored in binary. Inside your computer, there are no decimal values, there are no hex values, there are no octal values, there are no values in base 4 nor in base 6. Everything is in binary, base 2.

When you input a decimal value to your computer, in reality you are inputting a character string which represents a decimal value. What your program does when it receives that character string is that it converts it to the value that it represents in binary. When you output a decimal value, in reality you convert that binary value to a character string, which is what you end up outputting and displaying.

So instead of trying to copy a binary value to another binary variable, shouldn't you instead want to create a character string that represents a binary value in base 6? Not unlike how that function you posted, base4code, generated a character string representation of the value in binary (so why give it such a misleading name?).

You have to think about just exactly what it is that you are trying to do.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Sep 2013
Posts
22
Rep Power
0
i try to convert decimal number to base 6

like that 100 at base 10 is 244 on base 6
134 is 342
6. Again: WHAT ARE YOU TRYING TO DO?

What part of "binary is binary is binary is binary is ... " do you not understand?

What number base is the value of the variable, decimal, in?
What number base is the value of the variable, result, in?
Last edited by dwise1_aol; November 8th, 2013 at 11:45 AM.
7. All stored integer values are in binary, base 6, base 4, decimal or whatever is merely a matter of human readable output. You can convert and integer to a numeric string representation in any base, but that string is no longer an arithmetic object, it is just a string to which a human might apply some semantics.

The upshot is unless your algorithm generates a string it is doing nothing useful.

You are not performing a decimal to base 6 conversion unless both the output and the input are strings. If you pass an integer to your function you are converting from an integer to a base 6 string.

Your algorithm merely rearranges the bits of the integer in a way that makes very little sense, except perhaps that a base 4 digit value requires exactly 2 bits to represent, so a binary coded base 4 encoding would be exactly the same as the original binary encoding!

You could in principle define a binary encoded base 6 (on the same principle as binary coded decimal), but it would serve little purpose.

You could even as I suspect you are trying to do, generate a binary number that when printed as a decimal number displays the same digits that would be displayed if the original number were printed as base 6. That's some funky and unusual requirement, but perhaps a legitimate and interesting academic exercise, but one which you need to clearly define. But be clear that is not really decimal to base 6 conversion, because the resultant integer value will have a different arithmetic value, and remains binary in any case.

Here is a solution that does just what I described above - i.e. takes an integer and generates another integer than when printed as decimal will display the base 6 digits of the original integer.

C Code:
```
int base6( int decimal )
{
int b6digits[16] ;
int d = 0 ;
int e = 1 ;
int i = 0 ;
int base6 = 0 ;

while( decimal > 0 )
{
b6digits[d] = decimal % 6 ;
decimal /= 6 ;
d++ ;
}

for( i = 0; i < d; i++ )
{
base6 += b6digits[i] * e ;
e *= 10 ;
}

return base6 ;
}

#include <stdio.h>

int main()
{
printf( "%d", base6( 100 ) ) ;
}```
Last edited by clifford; November 8th, 2013 at 02:41 PM.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
13
Rep Power
0
I'll think about this more and see if I can up with a better answer. But my first thought is that you will probably want to make a class (to define a new type for base-6 numbers).

I think you would want to overload all the operators. Ie, I don't think there is any way around the fact that you will have to use strings when outputting it, but you could also have the integer value stored (as a decimal as far as we are reading it) for each number and overload the + operator to add the two int values and have the resulting object also calculate how to display the value in base 6. Hopefully that makes sense.

for example
Code:
```class base4number{
int value; // ie, 100
string display; ie, "244"
}```
Then include a function to make sure the display is calculated right, which shouldn't be too hard once you decide you're going to have to have it be a string.