Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
Oh, you mean having an output array like:

char[] output = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', ... , 'Z' };

And when you calculate each output component in the output base, you then use it as an index into this table? Like if the value was 227 (base10) and the output was in base 14, so we extract out component values of: 196 + 28 + 3 (base 10) or 1x14^2 + 2x14^1 + 3x14^0. So we feed in 1,2,3 into the table, and get out '1', '2', '3'?

Or do you mean that we have:

char output[NUM_BASES][NUM_POWERS_OF_BASE][VALUES] ?

Because we'd still have to worry about the size of NUM_POWERS_OF_BASE, which would be tied to the output string length.

I guess I'm not making myself clear. The idea would be that he work with one digit at a time and its value is referred to in an array. But, he'd know which stage in the process he was in (^0, ^1, ^2, etc) to modify that value. This gives you an upper limit on the array size. Obviously using an array in the way I suggested would ahve lots of wasted space for lower bases.

If I wasn't in a meeting right now I'd type something up. :p
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
I solved the problem.

Because we couldn't use strings, I was told to make my number character array very large, (200).

This let to many weird data sets in my array. I attempted to use isalnum on each entry, but Visual Studio kept crapping out with "unknown errors". :mad:

So I sent each array entry through an already made function that basically checked to see if it was 09 or A-Z. If it wasn't, I just returned a -1.

When it's time to print out the array, if the return from the test isn't -1, then it's ok. :D

Easy. (this assignment is due for over another week. lol)
 

Palad1

macrumors 6502a
Feb 24, 2004
647
0
London, UK
arrays

Well, we can convert your elegant arrayless version into an elegant array full version ;)

<code lang="pseudo_c" quality="www.thedailywtf.com">
PHP:
#include "elegant.h" /* let's assume we have our int readFromBase(int base,char** string, int stringLength) elegant method defined in there*/

typedef int (*readFrombaseFunc)(int, char**, int);
typedef struct (readFromBaseFunc func,int base} converter;

converter[17] converters={
  {null,null}, 
  {null,null},
  {&readFromBase,2},
  {&readFromBase,3},
  {&readFromBase,4},
  {&readFromBase,5},
  {&readFromBase,6},
  {&readFromBase,7},
  {&readFromBase,8},
  {&readFromBase,9},
  {&readFromBase,10},
  {&readFromBase,11},
  {&readFromBase,12},
  {&readFromBase,14},
  {&readFromBase,15},
  {&readFromBase,16}
 };
int convertWithArrays(int fromBase,char**string, int stringLength)
{
  converter conv=converters[fromBase];
  if(conv.func){
    return (*conv.func)(conv.base,string,stringlength);
 }else{
   errno=1004577;
   return -1;
 }
}
</code>

Function pointers FTW!
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
Function pointers FTW!

I love function pointers. I often use an array of two function pointers and index them with a boolean expression in hard realtime applications because it's a cross-platform way to add determinism.
 

bobber205

macrumors 68020
Original poster
Nov 15, 2005
2,182
1
Oregon
Well. My project is done at last.. I wanted to be sure I got all the docs done with it before posting it here. I do know there's some debug output but I didn't think you guys would mind.

I'm not ready to get rid of it yet.
 

Attachments

  • code.txt
    10.9 KB · Views: 126

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
Toronto, Canada
Minor detail, but you're not converting to base 10 as an intermediary step. You're summing the components in int variables, which means, you're converting them to binary, not base 10. True, if you then print out those ints, they will show up as base 10, but that's only because the print routines enterpret the binary ints, and display them in base 10.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.