Sunday, July 8, 2007

Write a C progam to convert from decimal to any base (binary, hex, oct etc...).

Here is some really cool C code


#include < stdio.h >

int main()
{
decimal_to_anybase(10, 2);
decimal_to_anybase(255, 16);
getch();
}

decimal_to_anybase(int n, int base)
{
int i, m, digits[1000], flag;
i=0;

printf("\n\n[%d] converted to base [%d] : ", n, base);

while(n)
{
m=n%base;
digits[i]="0123456789abcdefghijklmnopqrstuvwxyz"[m];
n=n/base;
i++;
}

//Eliminate any leading zeroes
for(i--;i >= 0;i--)
{
if(!flag && digits[i]!='0')flag=1;
if(flag)printf("%c",digits[i]);
}
}

13 comments:

Anonymous said...

I could get your code to work.
It seemed to get stuck doing the same thing over, meaning result was the number 9 for each base.

Here is my example fixing yours, but with changes that made more sense to mea and I think make the code more understandable as to what it is doing.

#include stdio.h /*altered tag*/
void decimalToAnyBase(int, int);
main()
{
int num, base;

while (scanf("%d %d",&num, &base) == 2)
{
printf("%d converted to base %d\n",num,base);
decimalToAnyBase(num,base);
putchar('\n');
}

return 0;
}

/* define the decimalToAnyBase() function */
void decimalToAnyBase(int n, int b)
{
int i, m, digits[1000], flag;

i = 0;
m = n % b;

// Keep going if number 'n' is > the base and not zero
if (n >= b)
{
digits[i]="0123456789abcdefghijklmnopqrstuvwxyz"[m];
printf("digits[%d] = %d.%d\t\tn = %d\n",i,digits[i],m,n);
n = n / b;
i++;
decimalToAnyBase(n,b); /* recursive call */
} else if (n != 0) {
digits[i]="0123456789abcdefghijklmnopqrstuvwxyz"[m];
printf("digits[%d] = %d.%d\t\tn = %d\n",i,digits[i],m,n);
n = n / b;
i++;
decimalToAnyBase(n,b);
}

Hope this helps...
Mark (medeepthinker)

medeepthinker said...

Hey I think I just managed to sigh up. Cool!

Anonymous said...

Bonjorno, vijayinterviewquestions.blogspot.com!
[url=http://viagraenat.pun.pl/ ]Comprare viagra generico[/url] [url=http://cialisdkee.pun.pl/ ]Compra cialis generico[/url] [url=http://viagraycla.pun.pl/ ]Compra viagra generico[/url] [url=http://cialisonya.pun.pl/ ]Compra cialis generico[/url] [url=http://viagrareta.pun.pl/ ]Acquistare viagra generico[/url] [url=http://cialisybea.pun.pl/ ]Vendita cialis online[/url]

Anonymous said...

How can i remove windows xp from my laptop and reinstall windows Me -the laptops original software?
I suffer with recently bought a familiar laptop that is old. The himself I had bought it from had installed windows xp on it, even though it originally came with windows Me. I be to oust the windows xp because it runs slows on the laptop because it takes up more honour than the windows Me would. Also I want to remove windows xp because it is an forbidden copy. So when I tried to run updates on it, windows would not initiate updates because the windows xp is not genuine. [URL=http://fhmesoa.hostific.com]air brush for finger nails[/URL]
----------------------------------------------------------------------

Answers :

It's best to take one's leave of [URL=http://mpgyvib.instantfreehosting.com/remington-700-bolt-handle-locking-safety.html]remington 700 bolt handle locking safety[/URL] Windows XP and even-handed upgrade your laptop. It's much better. [URL=http://noxynjc.hostific.com/mcafee-superdat.html]mcafee superdat[/URL] Besides, Windows XP is style [URL=http://phckoke.hostific.com/justin-toper.html]justin toper[/URL] healthier then Windows Me. Windows Me is d‚mod‚ and multifarious programs that can come across with XP, can't [URL=http://wjevqea.instantfreehosting.com/55-homes-victorville-ca.html]55 homes victorville ca[/URL] vamoose with Me.
------------------------------
all you have to do is brochure the windows me disk into the cd drive. then reboot your laptop, when the coal-black [URL=http://kiafruu.hostific.com/big-rideau-lake-cottage-properties.html]big rideau lake cottage properties[/URL] shield with all the info comes up and when it asks u to boot from cd [URL=http://leuqaua.hostific.com/tushy-licking-blog.html]tushy licking blog[/URL] chance any indication when it tells you to then inaugurate from there !!! I RECOMEND SINCE ITS AN ILLEAGLE TEXT TO WIPE [URL=http://syaioag.hostific.com/ametrine-mens-ring.html]ametrine mens ring[/URL] MANIFEST THE [URL=http://zxrengh.hostific.com/roatan-resorts.html]roatan resorts[/URL] THOROUGH TIRING DRIVE WHEN IT ASKS YOU WHICH IMPENETRABLE [URL=http://xyeyjqa.hostific.com/chin-strap-adapter.html]chin strap adapter[/URL] GOAD TO POSITION IT ON. THEN SUM ALL THE UNENCUMBERED PAUSE ON THE EMPTY [URL=http://dfizone.hostific.com/tassimo-sweepstakes.html]tassimo sweepstakes[/URL] FLINTY DRIVE ONTO A DIFFERENT COLUMN SETTING, IT WILL-POWER LOOK LIKE C:/ Open or something like that

Anonymous said...

Non-malignant prostatic hyperplasia, commonly known as BPH, is an enlargement of the prostate area. It is more rampant in older men. As men are comely more cultivated about robustness issues, they convert to medical treatment for BPH. Dutas, a generic formation of Avodart([URL=http://jeqpqpv.1freewebspace.com/avodart-and-heart-problems.html]avodart and heart problems[/URL] [URL=http://jeqpqpv.1freewebspace.com/avodart-diarrhea-sideeffects.html]avodart diarrhea sideeffects[/URL] [URL=http://jeqpqpv.1freewebspace.com/avodart-prostate-reduction.html]avodart prostate reduction[/URL] [URL=http://jeqpqpv.1freewebspace.com/avodart-prostate.html]avodart prostate[/URL] [URL=http://jeqpqpv.1freewebspace.com/avodart-and-insomnia.html]avodart and insomnia[/URL] ), has been proven as an moving treatment of BPH. BPH and its symptoms that adversely upset the grade of lifestyle can be treated successfully nearby Dutas. The essential foretoken evidence of BPH is the frequency of requisite to urinate. This occurs chiefly at vespers all the time but then progresses to the have occasion for to urine again throughout the day. BPH sufferers subsequently circulate a reduction in power in urine stream. Trouble accompanies this reduction. A medical doctor should carry testing to determine if BPH is the grounds of the symptoms. The effectiveness of Dutas is found in the chemical compound Dutasteride. This active ingredient is an alpha-reductase 5 inhibitor which impedes the conversion of testosterone into dihydrotestosterone (DHT). DHT is considered a persuasive technique of testosterone. BPH symptoms vanish promptly the conversion is interrupted. Dutas has been base to be junk in BPH for many sufferers. Prescriptions finasteride and finasteride has been shown to at best curb at one isoform of alpha redictase 5. It has been established that Dutasteride has been proven to hold back two isoforms. Dutas incontestably appears to furnish the pre-eminent treatment close by seeking BPH. Dutas press be charmed as directed with some precautions. Erectile dysfunction and decreased genital libido are the most commonly reported side effects during routine of Dutas. Gynecomastia or enlargement of manly heart network is another accomplishable side effect. Additionally, women who are productive or women inferior to be proper pregnant should not be exposed to Dutas; developing masculine fetuses can be adversely pretentious before these inhibitors. Dutas can be engaged wholly the fell so individual dolour should be exercised in regard to expressive women or women second-rate to ripen into pregnant. Another side effect of Dutas is a favourable one. Some men possess reported hair replenishment while enchanting Dutas. BPH can be treated by discussing medications and feasible side effects with a medical professional. Dutas can give impressive treatment of BPH. A worry-free, potent life is justly quality the effort.
[URL=http://jeqpqpv.1freewebspace.com/avodart-and-lower-psa.html]avodart and lower psa[/URL]
[URL=http://jeqpqpv.1freewebspace.com/avodart-effectiveness-hair-loss.html]avodart effectiveness hair loss[/URL]
[URL=http://jeqpqpv.1freewebspace.com/avodart-for-hair-loss-study.html]avodart for hair loss study[/URL]
[URL=http://jeqpqpv.1freewebspace.com/avodart-psa.html]avodart psa[/URL]
[URL=http://jeqpqpv.1freewebspace.com/avodart-medicine.html]avodart medicine[/URL]

subburajr said...

int convert(unsigned int num, int base)
{
static char buf[33];
char *ptr;
int i = 0,j;

ptr=&buf[sizeof(buf)-1];
*ptr='\0';

do
{
*--ptr="0123456789abcdef"[num%base];
num/=base;
}while(num!=0);

for(j = 0; j <= strlen(ptr); j++){

if( ('0' <= ptr[j]) && ('9' >= ptr[j]))
i = (i<<4)+(ptr[j]-'0');
else if(('a' <= ptr[j]) && ('f' >= ptr[j]))
i = (i<<4) + (ptr[j]-'a') + 0xa;
else if(('A' <= ptr[j]) && ('F' >= ptr[j]))
i = (i<<4)+ (ptr[j]-'A') + 0xa;
}
return i;
}

Anonymous said...

post66, http://www.rc.umd.edu/cstahmer/cogsci/ viagra pro, htmf3, http://www.rc.umd.edu/ order viagra

Anonymous said...

I love vijayinterviewquestions.blogspot.com! Here I always find a lot of helpful information for myself. Thanks you for your work.
Webmaster of http://loveepicentre.com and http://movieszone.eu
Best regards

Anonymous said...

I love dis site. It's indeed very helpful n knowledgeable. Now i'm clear in many of my logics.

Anonymous said...

best place to buy xanax online 4mg xanax and alcohol - caffeine pills xanax

Anonymous said...

clomid clomiphene | how to buy clomid online - can i buy clomid over the counter, clomid twins rate

Anonymous said...

Υоur сurrent artіcle has verified necеssary to mе.
Ӏt’s reаlly hеlpful аnd you're naturally very well-informed of this type. You possess opened up my own face to varying views on this subject matter along with intriguing and solid written content.

My webpage; Klonopin
Also visit my web blog ; Klonopin

Anonymous said...

Hello colleagues, how is all, and what you want to say concerning this post,
in my view its actually remarkable for me.


Feel free to surf to my page tringle à rideaux pour fenetre pvc