Domanda

Fa .NET hanno una funzione nativa che è equivalente a PHP base_convert o devo scrivere il mio?Voglio convertire da qualsiasi base per qualsiasi altra base, dove la 'di base' o 'da' base può essere un numero intero 2-36.

Esempio di funzione PHP:base_convert($number_to_convert, $from_base, $to_base)

// convert 101 from binary to decimal
echo base_convert('101', 2, 10);
// 5

Come notato da Luca nei commenti di Jon Skeet risposta:La conversione.ToString non in grado di gestire la conversione a/da qualsiasi arbitraria base 2, 8, 10 e 16

Aggiornamento: A quanto pare, la risposta è:no, non c'è modo nativo.Di seguito, Erik mostra un modo per fare questo.Un'altra implementazione è qui: http://www.codeproject.com/KB/macros/Convert.aspx

È stato utile?

Soluzione

Ecco il codice che potrai convertire un intero in un arbitrario base fino a 36, e la conversione di una rappresentazione in forma di stringa di una base x il valore di un numero intero (data base):

class Program {
    static void Main(string[] args) {
        int b10 = 123;
        int targetBase = 5;

        string converted = ConvertToBase(b10, targetBase);
        int convertedBack = ConvertFromBase(converted, targetBase);

        string base3 = "212210";
        string base7 = ConvertFromBaseToBase(base3, 3, 7);

        Console.WriteLine(converted);
        Console.WriteLine(convertedBack);
        Console.WriteLine(base7);
        Console.ReadLine();
    }

    private const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static string ConvertToBase(int b10, int targetBase) {
        if (targetBase < 2) throw new ArgumentException("Target base must be greater than 2.", "targetBase");
        if (targetBase > 36) throw new ArgumentException("Target base must be less than 36.", "targetBase");

        if (targetBase == 10) return b10.ToString();

        StringBuilder result = new StringBuilder();

        while (b10 >= targetBase) {
            int mod = b10 % targetBase;
            result.Append(chars[mod]);
            b10 = b10 / targetBase;
        }

        result.Append(chars[b10]);

        return Reverse(result.ToString());
    }

    private static int ConvertFromBase(string bx, int fromBase) {
        if (fromBase < 2) throw new ArgumentException("Base must be greater than 2.", "fromBase");
        if (fromBase > 36) throw new ArgumentException("Base must be less than 36.", "fromBase");

        if (fromBase == 10) return int.Parse(bx);

        bx = Reverse(bx);
        int acc = 0;

        for (int i = 0; i < bx.Length; i++) {
            int charValue = chars.IndexOf(bx[i]);
            acc += (int)Math.Pow(fromBase, i) * charValue;
        }

        return acc;
    }

    public static string ConvertFromBaseToBase(string bx, int fromBase, int toBase) {
        int b10 = ConvertFromBase(bx, fromBase);
        return ConvertToBase(b10, toBase);
    }

    public static string Reverse(string s) {
        char[] charArray = new char[s.Length];
        int len = s.Length - 1;
        for (int i = 0; i <= len; i++)
            charArray[i] = s[len - i];
        return new string(charArray);
    }
}

Se sei indifferente con la visualizzazione di questi valori, è possibile utilizzare caratteri estesi nel set di caratteri - se il bastone di testo ascii, in teoria è possibile avere base256 valori.Andando al di là che avrei consiglia di non utilizzare caratteri, ma invece di utilizzare un altro univocamente identificabili valore - anche se non ho molto vedere il valore.

Altri suggerimenti

EDIT: Questa risposta è molto conveniente, ma funziona solo per le basi 2, 8, 10 e 16

È possibile utilizzare Convert.ToInt32(text, base) e quindi Convert.ToString(number, base) :

using System;

class Test
{
    static void Main()
    {
        int number = Convert.ToInt32("101", 2);
        string text = Convert.ToString(number, 10);
        Console.WriteLine(text); // Prints 5
    }
}

Se si sta convertendo o dalla base 10, non è necessario specificare che -. È il default

Si noti che questo funziona solo per le basi 2, 8, 10 e 16. Se si desidera qualsiasi altra cosa, dovrete scrivere il proprio parser / formattatore.

Nel ConvertToBase, la seguente riga:

mentre (B10> targetBase)

... dovrebbe essere:

mentre (b10> = targetBase)

Quale cura il numero di base spuntare nel numero convertito (per esempio la conversione "3" nella base 3 rese "3" invece di "10").

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top