简体   繁体   English

有没有内置的方法将整数转换为C#中的字符串(任何基数)?

[英]Is there any built-in way to convert an integer to a string (of any base) in C#?

Convert.ToString() only allows base values of 2, 8, 10, and 16 for some odd reason; 由于某些奇怪的原因,Convert.ToString()只允许基数值为2,8,10和16; is there some obscure way of providing any base between 2 and 16? 是否有一些不明确的方式提供2到16之间的任何基础?

Probably to eliminate someone typing a 7 instead of an 8, since the uses for arbitrary bases are few (But not non-existent). 可能会消除某人输入7而不是8,因为任意碱基的使用很少(但不是不存在)。

Here is an example method that can do arbitrary base conversions. 这是一个可以执行任意基本转换的示例方法。 You can use it if you like, no restrictions. 如果您愿意,可以使用它,没有限制。

string ConvertToBase(int value, int toBase)
{
     if (toBase < 2 || toBase > 36) throw new ArgumentException("toBase");
     if (value < 0) throw new ArgumentException("value");

     if (value == 0) return "0"; //0 would skip while loop

     string AlphaCodes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

     string retVal = "";

     while (value > 0)
     {
          retVal = AlphaCodes[value % toBase] + retVal;
          value /= toBase;
     }

     return retVal;
}

Untested, but you should be able to figure it out from here. 未经测试,但你应该能够从这里弄清楚。

Sorry, I'm not answering your question but... The choice of bases is not arbitary. 对不起,我没有回答你的问题,但......基地的选择不是仲裁。 You pc is constantly converting from base 2 (it's internal binary system) to the human readable base 10. Base 8 and 16 are very easy to convert to and from base 2 and are often used so a computer AND a human can read the value (eg GUIDs) 你的电脑不断从基座2(它的内部二进制系统)转换到人类可读的基座10.基座8和16非常容易转换到基座2和从基座2转换,并且经常被使用,因此计算机和人类可以读取值(例如GUID)

You could try the following: 您可以尝试以下方法:

http://www.dotnetspider.com/resources/938-Conversion-Decimal-number-any-Base-vice.aspx http://www.dotnetspider.com/resources/938-Conversion-Decimal-number-any-Base-vice.aspx

This at least gives the impression that you could have any base (from 2->16). 这至少给人的印象是你可以有任何基础(从2> 16)。 Although Im a little confused as to why you would want to ! 虽然我有点困惑,为什么你想要!

//untested  -- public domain
// if you do a lot of conversions, using StringBuilder will be 
// much, much more efficient with memory and time than using string
// alone.

string toStringWithBase(int number, int base)
    { 
    if(0==number) //handle corner case
        return "0";
    if(base < 2)
        return "ERROR:  Base less than 2";

    StringBuilder buffer = new StringBuilder(); 

    bool negative = (number < 0) ? true : false;
    if(negative)
        {
        number=-number;
        buffer.Append('-');
        }

    int digits=0;
    int factor=1;

    int runningTotal=number;
    while(number > 0)
       {
       number = number/base;
       digits++;
       factor*=base;
       }
    factor = factor/base;

    while(factor >= 1)
       {
       int remainder = (number/factor) % base;

       Char out = '0'+remainder;
       if(remainder > 9)
           out = 'A' + remainder - 10;
       buffer.Append(out);
       factor = factor/base;
       }

    return buffer.ToString
    }
string foo = Convert.ToString(myint,base);

http://msdn.microsoft.com/en-us/library/14kwkz77.aspx http://msdn.microsoft.com/en-us/library/14kwkz77.aspx

EDIT : My bad, this will throw an argument exception unless you pass in the specified bases (2, 8, 10, and 16) 编辑 :我的坏,这将引发一个参数异常,除非你传入指定的基数(2,8,10和16)

Your probably SOL if you want to use a different base (but why???). 你可能是SOL,如果你想使用不同的基础(但为什么???)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM