By using this site, you acknowledge that you have read and understand our Cookie policy, Privacy policy and Terms .
access_time 10 years ago comment 0 language中文
visibility 797 arrow_upward arrow_downward

在我们的一个系统中,为了更方便以及用更少的字符表示更大的数字,在C#中实现了32进制的结构Duotricemary,也可将其更改为类。

它主要包含的功能为:通过字符串比如“2EWS”创建三十二进制的实例,也可以通过整型比如12000创建实例,既可以把数字以三十二进制的字符表述,也可以三十二进制的字符转换为整型;添加了从string以及int类型强制转换的隐式操作符,也添加了加减操作符,其它功能可以自行根据需要添加。

注意:其中的一些算法可以修改精简等,以提高计算的性能。

这个类的源代码如下

/*C#实现三十二进制
All Rights Reserved by Raymond.
http://hi.baidu.com/1987raymond
china.raymond[at]hotmail.com*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PolarLight.XPlatform.Common
{
    /// <summary>
    /// 三十二进制
    /// </summary>
    /// <remarks>更多的方法可以创建,比如两个三十二进制数相减等等</remarks>
    [Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
    public struct Duotricemary
    {
        #region 字段和属性
        /// <summary>
        /// 代表字符串格式的三十二进制所有组成的字符
        /// </summary>
        const string CHARS = "0123456789ABCDEFGHJKLMNPQRTUVWXY";

        private string m_StringValue;
        /// <summary>
        /// 字符串格式的值
        /// </summary>
        /// <remarks></remarks>
        public string StringValue
        {
            get { return m_StringValue; }
            set { m_StringValue = value; }
        }

        private uint? m_IntValue;
        /// <summary>
        /// 十进制的值
        /// </summary>
        public uint? IntValue
        {
            get { return m_IntValue; }
            set { m_IntValue = value; }
        }
        #endregion

        #region 构造函数与创建实例的静态方法等
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="stringValue"></param>
        public Duotricemary(string stringValue)
        {
            m_StringValue = stringValue;
            m_IntValue = null;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="intValue"></param>
        public Duotricemary(uint intValue)
        {
            m_IntValue = intValue;
            m_StringValue = null;
        }

        /// <summary>
        /// 通过字符串比如EG创建三十二进制的实例
        /// </summary>
        /// <param name="stringValue"></param>
        /// <returns></returns>
        public static Duotricemary FromString(string stringValue)
        {
            return new Duotricemary(stringValue);
        }

        /// <summary>
        /// 通过非负整形创建三十二进制的实例
        /// </summary>
        /// <param name="intValue"></param>
        /// <returns></returns>
        public static Duotricemary FromInt(uint intValue)
        {
            return new Duotricemary(intValue);
        }
        #endregion

        #region 转换方法

        /// <summary>
        /// 转换为无符号整型类型
        /// </summary>
        /// <returns></returns>
        public uint ToInt()
        {
            if (!this.IntValue.HasValue)
            {
                this.IntValue = this.ToInt(this.StringValue);
            }
            return this.IntValue.Value;
        }

        /// <summary>
        /// 将字符串表示的三十二进制数字转换为整形
        /// </summary>
        /// <param name="stringValue"></param>
        /// <returns></returns>
        private uint ToInt(string stringValue)
        {
            uint value = 0;
            if (!string.IsNullOrEmpty(stringValue))
            {
                int j = 0;
                for (int i = stringValue.Length; i > 0; i--, j++)
                {
                    char c = stringValue[i - 1];
                    int index = Duotricemary.CHARS.IndexOf(c);
                    if (index == -1)
                    {
                        throw new FormatException("Unrecognizable duotricemary format.");
                    }
                    value += (uint)(Math.Pow(32, j) * (index));

                }
            }
            return value;
        }

        /// <summary>
        /// 将十进制非负Int值转换为三十二进制
        /// </summary>
        /// <param name="intValue"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        private string ToDuotricemaryString(uint intValue)
        {
            if (intValue < 32)
            {
                return intValue.ToString();
            }
            else
            {
                Stack<char> chars = new Stack<char>();
                uint temp = intValue;
                int n = 0;
                while (temp > 0)
                {
                    n = (int)(temp % 32);
                    temp = (uint)(temp / 32);
                    chars.Push(Duotricemary.CHARS[n]);
                }
                StringBuilder sb = new StringBuilder();
                int totalChars = chars.Count;
                for (int j = 0; j < totalChars; j++)
                {
                    sb.Append(chars.Pop());
                }
                return sb.ToString(); ;
            }
        }
        #endregion

        #region 操作符
        ///// <summary>
        ///// 将uint类型强制转换为Duotricemary
        ///// </summary>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //public static explicit operator Duotricemary(uint value)
        //{
        //    return new Duotricemary(value);
        //}

        /// <summary>
        /// 将uint类型隐式转换为Duotricemary
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator Duotricemary(uint value)
        {
            return new Duotricemary(value);
        }

        ///// <summary>
        ///// 将字符串类型强制转换为Duotricemary
        ///// </summary>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //public static explicit operator Duotricemary(string value)
        //{
        //    return new Duotricemary(value);
        //}

        /// <summary>
        /// 将字符串类型隐式转换为Duotricemary
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static implicit operator Duotricemary(string value)
        {
            return new Duotricemary(value);
        }

        /// <summary>
        /// 加
        /// </summary>
        /// <param name="d"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Duotricemary operator +(Duotricemary d, uint value)
        {
            uint i = d.ToInt();
            return new Duotricemary(i + value);
        }

        /// <summary>
        /// 减
        /// </summary>
        /// <param name="d"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Duotricemary operator -(Duotricemary d, uint value)
        {
            uint i = d.ToInt();
            return new Duotricemary(i - value);
        }

        #endregion

        #region 方法重载
        /// <summary>
        /// 重载ToStirng方法
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (string.IsNullOrEmpty(this.StringValue))
            {
                if (this.IntValue.HasValue)
                {
                    this.StringValue = this.ToDuotricemaryString(this.IntValue.Value);
                }
                else
                {
                    this.IntValue = 0;
                    this.StringValue = "0";
                }
            }
            return this.StringValue;
        }
        #endregion
    }
}

使用举例

using System;
using System.Collections.Generic;
using System.Text;
using PolarLight.XPlatform.Common;

namespace DuotricemaryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Duotricemary> duos = new List<Duotricemary>();

            Duotricemary duo = (Duotricemary)32;
            duos.Add(duo);
            Duotricemary duo1 = new Duotricemary("DFGF");
            duos.Add(duo1);
            Duotricemary duo2 = Duotricemary.FromInt(22211);
            duos.Add(duo2);
            Duotricemary duo3 = Duotricemary.FromString("A43B2");
            duos.Add(duo3);
            Duotricemary duo6 = new Duotricemary(10620258);
            duos.Add(duo6);
            //加减操作
            Duotricemary duo4 = duo3 + 100;
            duos.Add(duo4);
            Duotricemary duo5 = duo4 - 20;
            duos.Add(duo5);
            //隐式强制转换
            Duotricemary duo7 = "0000DDW";
            duos.Add(duo7);
            //隐式强制转换
            Duotricemary duo8 = 13757;
            duos.Add(duo8);

            foreach (Duotricemary d in duos)
            {
                OutputDuotricemary(d);
            }

            Console.ReadLine();
        }

        static void OutputDuotricemary(Duotricemary duo)
        {
            Console.WriteLine("\"{0}\"={1}",duo.ToString(),duo.ToInt());
            Console.WriteLine();
        }
    }
}

输出结果

"10"=32
"DFGF"=441871
"MN3"=22211
"A43B2"=10620258
"A43B2"=10620258
"A43E6"=10620358
"A43DJ"=10620338
"0000DDW"=13757
"DDW"=13757

info Last modified by Raymond at 11 days ago

Please log in or register to comment. account_circle Log in person_add Register
comment Comments (0)
No comments yet.
account_circle Raymond

About C# Programming Language: features, and syntax and so on.

open_in_new View

info License/Terms