473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Convert.ToString() Base Limitations

Does anyone know why the toBase argument in Convert.ToString() is limited to
2, 8, 10, or 16? It takes virtually the same code to support all base values
from 2 to 36.

And can anyone tell me if the .NET library provides the means to convert a
number to a string using base-36?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
Nov 16 '06 #1
4 8126
Jonathan Wood <jw***@softcircuits.comwrote:
Does anyone know why the toBase argument in Convert.ToString() is limited to
2, 8, 10, or 16? It takes virtually the same code to support all base values
from 2 to 36.

And can anyone tell me if the .NET library provides the means to convert a
number to a string using base-36?
I haven't seen such a call anywhere in the library, but it wouldn't be
hard to write an arbitrary conversion which either took just a base and
used the alphabet, or took a character array or string where each
character is a "digit" to use in the output, where the base is just the
length of the array/string.

If you want, I could try to put that into my MiscUtil library over the
weekend...

--
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
Nov 16 '06 #2
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";

string UIntToString(UInt32 value, UInt32 toBase)
{
string result = "";

do
{
UInt32 digitValue = (value % toBase);
result += _alphaDigits[(int)digitValue];
value /= toBase;
} while (value != 0);
result = ReverseString(result);
return result;
}

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;
}

string ReverseString(string s)
{
string result = "";

for (int i = 0; i < s.Length; i++)
result = s.Substring(i, 1) + result;
return result;
}

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Jonathan Wood <jw***@softcircuits.comwrote:
>Does anyone know why the toBase argument in Convert.ToString() is limited
to
2, 8, 10, or 16? It takes virtually the same code to support all base
values
from 2 to 36.

And can anyone tell me if the .NET library provides the means to convert
a
number to a string using base-36?

I haven't seen such a call anywhere in the library, but it wouldn't be
hard to write an arbitrary conversion which either took just a base and
used the alphabet, or took a character array or string where each
character is a "digit" to use in the output, where the base is just the
length of the array/string.

If you want, I could try to put that into my MiscUtil library over the
weekend...

--
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

Nov 16 '06 #3
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
Nov 16 '06 #4
Thanks for the feedback. I've printed your post and will go over it
carefully later.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
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

Nov 16 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Soon Lee | last post by:
ANy one know of any fast method in converting dec to hex or bin? -- WebSite : http://soonlee.agreatserver.com
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
4
by: CSharpener | last post by:
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C# In JavaScript, it goes like this for an int: javascript:(123).toString(2 or...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
2
by: Tedmond | last post by:
Can anyone tell me how to convert a byte to bit pattern? e.g. Byte b = 1; after conversion = 00000001 Tedmond
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.