示例#1
0
        //------------------------------------------------------------
        // convert into a string
        public override string ToString(uint Base)
        {
            // first the sign
            string str = (_Sign) ? "-" : "";

            // numbers of digits separated by '.'
            int step = 4;

            // now the base-indication
            switch (Base)
            {
            case 2: str += "0b"; break;

            case 8: str += "0o"; break;

            case 10: step = 3;  break;

            case 16: str += "0x"; break;

            default: str += '[' + Base.ToString() + ']'; break;
            }

            // if the base is a power of 2
            // let's calc the binary logarithm
            bool poweroftwo = ((Base & (Base - 1)) == 0);

            if (poweroftwo)
            {
                uint b = Base;
                Base = 0;
                while (b > 1)
                {
                    b >>= 1; Base++;
                }
                ;
            }

            // generate reversed string of digits
            CNumber_Integer tmp = (CNumber_Integer)Clone();
            string          num = String.Empty;
            int             cnt = 0;

            do
            {
                if (cnt == step)
                {
                    cnt = 0; num += '.';
                }
                uint rem = (poweroftwo) ? tmp.ShiftRight((int)Base) : tmp.Divide(Base);
                num += (char)((rem > 9) ? (rem - 10 + 'A') : (rem + '0'));
                cnt++;
            } while (!tmp.IsZero);

            for (int i = num.Length - 1; i >= 0; i--)
            {
                str += num[i];
            }
            return(str);
        }
示例#2
0
        //------------------------------------------------------------
        // create a string to a base of a power of 2
        protected string toString_2ish_base(uint Base)
        {
            uint bits = log_bin(Base);

            // generate reversed string of digits
            CNumber_Integer tmp = (CNumber_Integer)Clone();
            string          num = String.Empty;
            int             cnt = 0;

            do
            {
                if (cnt == 4)
                {
                    cnt = 0; num += '.';
                }
                uint rem = tmp.ShiftRight(bits);
                num += (char)((rem > 9) ? (rem - 10 + 'A') : (rem + '0'));
                ++cnt;
            } while (!tmp.IsZero);
            return(num);
        }