-
loop with divition
im tring to code a program that will convert decimals in to binaries... but i need it to keep dividing the numbers untill 1 or 0 or untill it cant divide anymore.. but i cant get it to work :( any help?
Code:
int binary( int decimal )
{
int x, y;
char remainder[10];
char converted[10];
for( x = 0; x < decimal; x++ )
{
x = ( decimal / BASE_2 );
*remainder = decimal % x;
strcpy( converted, remainder );
printf( "%d\n", converted[x] );
}
// return x;
}
-
Try using something like this instead
Code:
#include <stdio.h>
#include <limits.h>
char *convert(unsigned int);
int main(void)
{
printf("%s\n", convert(150));
return 0;
}
char *convert(unsigned int num)
{
static char dest[33];
int count;
unsigned int base= UINT_MAX;
int ignoreflag=1;
for ( count=0; count<32 && num>0; count++, base /= 2)
{
if ( num >= base )
{
dest[count] = '0' + 1;
num -= base;
ignoreflag=0;
}
else
if ( !ignoreflag )
dest[count] = '0';
else
count--;
}
return dest;
}
-
thank you Thantos, but im still new to c.. do you mind making it a bit more simple for me if you dont mind please
-
Sure, give me a couple mins to fix the error I found and add some comments.
Edit ok here you go:
Code:
#include <stdio.h>
#include <limits.h>
char *convert(unsigned int);
int main(void)
{
char *converted;
converted = convert (150); /* convert 150 to from base 10 to base 2 */
printf("%s\n", converted); /* Print the string */
return 0;
}
/* Returns the address of the static variable dest for use in the printf and whatnot */
char *convert(unsigned int num)
{
static char dest[33]; /* The character array that will hold the converted value.
It is static so we can pass the address freely */
int count; /* Counter to so we can go through the array */
unsigned int base = (unsigned)INT_MAX + 1; /* The higheset possible power of 2 that can be stored in an unsigned number */
/* Populate the dest array with characters, go until num is no longer greater than 1 or we run out of room in the array
* (Should happen in a 32 bit system) */
for ( count=0; count<32; count++, base /= 2)
{
if ( num >= base ) /* If the number is equal to or greater than the base then we can take one out of the number for that power */
{
dest[count] = '0' + 1; /* To represent 1 as a character we add 1 to the character value of 0 */
num -= base; /* Subtract the base from the number */
}
else
dest[count] = '0'; /* if the number wasn't greater than or equal to the base then we put 0 */
}
return dest; /* return the address of the array. WARNING: This array will be overwritten if you call convert() again */
}
Advance note: For more portability you can use the following lines instead:
Code:
static char dest[(sizeof(int) * CHAR_BIT) + 1];
/* other code */
for ( count=0; count<sizeof dest - 1; count++, base /= 2)
/* rest of function */
-
-
a function which converted from any base to any base, would probably be more useful.
-
It actually only takes a few changes and that function can do any base conversion. This is due to the fact that to convert from base x to base y is actually a two step process. Step 1 is to convert from base x to internal represtation. Step 2 is to convert from internal represtation to base y. This function would be step 2.
Edit (Because I can't sleep atm):
Code:
char *convert(unsigned num, unsigned base_num)
{
static char dest[(sizeof(int) * CHAR_BIT) + 1];
int count, valcount;
unsigned base;
char basechars[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if ( base_num > sizeof basechars - 1 || base_num < 2)
{
printf("Requested base is outside of the range. Valid base ranges are 2 to %d\n", sizeof basechars - 1);
return NULL;
}
base = getMaxValBase(base_num);
for ( count=0; count<sizeof dest - 1 && num > 0 && base > 0; count++, base /= base_num)
{
for ( valcount=0; num >= base; valcount++)
num -= base;
dest[count] = basechars[valcount];
}
return dest;
}
unsigned getMaxValBase(unsigned base)
{
unsigned max = UINT_MAX / base;
unsigned val = base;
while ( val <= max)
val *= base;
return val;
}
Edit: Ok no idea why its putting a space between n and o but ignore it and realize that its not suppose to be there