Laden...

Zahlensystem Converter

Erstellt von Floste vor 15 Jahren Letzter Beitrag vor 15 Jahren 3.875 Views
Floste Themenstarter:in
1.130 Beiträge seit 2007
vor 15 Jahren
Zahlensystem Converter

Beschreibung:

Mit diesem Converter kann man Zahlen aus belibigen in beliebige Zahlensysteme Convertieren. Die Basis darf allerdings nicht größer als 256 sein. (Mit Convert.ToString geht nur 2, 8, 10, und 16 als Basis.)

Damit der Converter korrekt funzt, muss folgendes zutreffen: (alteBasis)(anzahl der Zeichen)<(264)/neueBasis
ODER alteBasisx=neueBasisy<(2^64)/neueBasis wobei x und y natürliche Zahlen ≥1 sein müssen.

Also nicht zu große Zahlen nehmen ODER z.B. vom 3er- ins 9er-System umwandeln.


public static class ZsW
    {
        public static byte[] WandleUm(byte[] ziffern, ushort alteBasis, ushort neueBasis)
        {
            int resultLänge = (int)Math.Ceiling(
                ((double)ziffern.Length) * Math.Log(alteBasis) / Math.Log(neueBasis)
                );
            byte[] neueZiffern = new byte[resultLänge];
            ulong potenzAlt = 1, potenzNeu = 1, a = 0;
            int iAlt = ziffern.Length - 1, iNeu = resultLänge - 1, exponentNeu = 0;
            while (iAlt >= 0)
            {
                while ((potenzAlt == 1 || potenzAlt != potenzNeu) && iAlt >= 0)
                {
                    a += ziffern[iAlt] * potenzAlt;
                    potenzAlt *= alteBasis;
                    if (ziffern[iAlt] >= alteBasis) throw new ArgumentException("Ziffer zu groß", "ziffern[" + iNeu.ToString() + "]");
                    iAlt--;
                    while (potenzNeu < potenzAlt) { potenzNeu *= neueBasis; exponentNeu++; }
                }
                if (potenzAlt != 1 && potenzAlt == potenzNeu)
                {
                    while (exponentNeu > 0)
                    {
                        ulong r = a % neueBasis;
                        neueZiffern[iNeu] = (byte)r;
                        a -= r;
                        a /= neueBasis;
                        exponentNeu--;
                        iNeu--;
                    }
                    potenzNeu = 1; potenzAlt = 1; exponentNeu = 0;
                }
            }
            while (a != 0)
            {
                ulong r = a % neueBasis;
                neueZiffern[iNeu] = (byte)r;
                a -= r;
                a /= neueBasis;
                iNeu--;
            }
            return neueZiffern;
        }

        public static string ToString(byte[] ziffern, ushort alteBasis, ushort neueBasis, char[] Alphabet)
        {
            byte[] res = WandleUm(ziffern, alteBasis, neueBasis);
            int i;
            for (i = 0; i < res.Length && res[i] == 0; i++)//nullen wegnehmen
            { }
            if (i == res.Length) return Alphabet[0].ToString();//das Ergebnis ist null.
            char[] s = new char[res.Length - i];
            while (i < res.Length)
            {
                s[i - res.Length + s.Length] = Alphabet[res[i]];
                i++;
            }
            return new string(s);
        }

        public static string ToString(byte[] ziffern, char[] Alphabet)
        {
            byte[] res = ziffern;
            int i;
            for (i = 0; i < res.Length && res[i] == 0; i++)//nullen wegnehmen
            { }
            if (i == res.Length) return Alphabet[0].ToString();//das Ergebnis ist null.
            char[] s = new char[res.Length - i];
            while (i < res.Length)
            {
                s[i - res.Length + s.Length] = Alphabet[res[i]];
                i++;
            }
            return new string(s);
        }

        public static byte[] ToNumerals(string value, ushort alteBasis, ushort neueBasis, char[] Alphabet)
        {
            int j = 0;
            byte[] res = new byte[value.Length];
            foreach (char n in value)
            {
                int i;
                for (i = 0; n != Alphabet[i]; i++)
                {
                    if (i > Alphabet.Length) throw new ArgumentException("ungültiges Zeichen: \"" + n.ToString() + "\"");
                }
                res[j++] = (byte)i;
            }
            return WandleUm(res,alteBasis,neueBasis);
        }

        public static byte[] ToNumerals(string value, char[] Alphabet)
        {
            int j = 0;
            byte[] res = new byte[value.Length];
            foreach (char n in value)
            {
                int i;
                for (i = 0; n != Alphabet[i]; i++)
                {
                    if (i > Alphabet.Length) throw new ArgumentException("ungültiges Zeichen: \"" + n.ToString() + "\"");
                }
                res[j++] = (byte)i;
            }
            return res;
        }
        public static string ToBase64String(byte[] data)
        {
            return ToString(data, 256, 64, Base64Alphabet);
        }

        public static byte[] FromBase64String(string data)
        {
            return ToNumerals(data, 64, 256, Base64Alphabet);
        }

        static Char[] Base64Alphabet = new Char[64]
            {
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    '0','1','2','3','4','5','6','7','8','9',
        '-','.'
            };

Schlagwörter: Zahlensystem Converter wandelt Zahlensysteme um Basis umwandeln base64 base 64 codieren

Projekte:Jade, HttpSaver
Zum Rechtschreiben gibts doch schon die Politiker. Aber die bauen auch nur mist!