krepta at juno.com (krepta at juno.com) wrote: > For what seems like a VERY long time now, I have been trying to create, > with my very limited programming knowledge and skill, a QBasic program > that could convert Decimal numbers to Binary,
#include <stdio.h> #include <stdlib.h> void reverse (char *); int main (int argc, char *argv[]) { int n = atoi(argv[1]); char s[33], *s1 = s; while (n) { *s1 = (n & 0x01) ? '1' : '0'; s1++; n >>= 1; } *s1 = '\0'; reverse(s); puts(s); return 0; } void reverse (char *s) { char *t = s; while (*t) t++; t--; while (s < t) { char c = *s; *s = *t; *t = c; s++; t--; } } > and vice versa. #include <stdio.h> int main (int argc, char *argv[]) { int n = 0; char *s = argv[1]; while (*s) { n = (n << 1) + (*s == '1'); s++; } printf ("%d\n", n); return 0; } > I gave up > on that project, but, I figured out how to program an XL Spreadsheet to > do the conversions for me. :) For some reason I was unable to make the > embeded IF statements go any deeper than 8 levels. Eight-level nested IF statements? Oh my. I really do *not* want to see that. > So, I had to put the > HEX converters on one set, and the OCT converters on another. I'll leave the base 16 and base 8 versions as exercises for the reader. And the QBasic port too. -- Greg Wooledge | "Truth belongs to everybody." greg at wooledge.org | - The Red Hot Chili Peppers http://wooledge.org/~greg/ | -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 240 bytes Desc: not available URL: <https://emu.freenetproject.org/pipermail/chat/attachments/20020330/55a5b9b1/attachment.pgp>