Jonathan Wood <jw***@softcircuits.comwrote:
Like I indicated before, the same code that generates base-2 or base-10
strings can also generate base-36 so I really don't understand the
limitation. Writing my own simply duplicates that code.
At any rate, I did write my own. I'm a long time programmer but new to C# so
if anyone sees anything I could've done better, I'm open to constructive
criticism.
Thanks.
static string _alphaDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Firstly, I'd make that a const string, and give it an appropriate
conventional name:
const sting AlphaDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string UIntToString(UInt32 value, UInt32 toBase)
I'd make that
static string UInt32ToString(uint value, uint toBase)
Making the type name the .NET name makes it easier to understand from
other languages, but using the C# aliases gives colouring in Visual
Studio.
{
string result = "";
do
{
UInt32 digitValue = (value % toBase);
result += _alphaDigits[(int)digitValue];
value /= toBase;
} while (value != 0);
result = ReverseString(result);
return result;
}
This is a bad way of building up a string - use StringBuilder instead.
See
http://www.pobox.com/~skeet/csharp/stringbuilder.html
In this particular case you'll never actually have much performance
penalty, but it's a good idea to get into good habits. It also means
you can reverse the contents of the StringBuilder in place.
UInt32 StringToUInt(string s, UInt32 toBase)
{
UInt32 result = 0;
for (int i = 0; i < s.Length; i++)
{
result *= toBase;
result += (UInt32)_alphaDigits.IndexOf(s[i]);
}
return result;
}
Other than the lack of error checking, this seems reasonable. (With the
same suggestions about naming, making it static etc)
>
string ReverseString(string s)
{
string result = "";
for (int i = 0; i < s.Length; i++)
result = s.Substring(i, 1) + result;
return result;
}
Again, I'd use StringBuilder - but I'd probably write it as:
static string ReverseString (string s)
{
return Reverse(new StringBuilder(s)).ToString();
}
// Note: returns the StringBuilder reference passed in for
// ease of method chaining - the contents are reversed
// in-place.
static StringBuilder Reverse (StringBuilder builder)
{
int lowIndex = 0;
int highIndex = builder.Length-1;
while (lowIndex <= highIndex)
{
char c = builder[lowIndex];
builder[lowIndex] = builder[highIndex];
builder[highIndex] = c;
lowIndex++;
highIndex--;
}
}
All of that is completely untested, by the way...
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog:
http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too