view source
print?
001usingSystem;
002usingSystem.Collections.Generic;
003usingSystem.Text;
004
005namespaceConsoleApplication2
006{
007publicstaticclassHexConversion
008{
009privatestaticstringThreeSix = "ABCDEFGHIJKLMNOPQISTUVWXYZ";
010
011#region 二进制转化
012/// <summary>
013/// 二进制到十进制
014/// </summary>
015/// <param name="num"></param>
016/// <returns></returns>
017publicstaticlongTwoToTen(stringnum)
018{
019returnConverHexToTen(num, 2);
020}
021
022/// <summary>
023/// 二进制到32进制
024/// </summary>
025/// <param name="number"></param>
026/// <returns></returns>
027publicstaticstringTwoTo32(stringnumber)
028{
029string[] str = GetByte(number, 5);
030StringBuilder num = newStringBuilder();
031foreach(strings instr)
032{
033longsix = TwoToTen(s);
034}
035returnnull;
036}
037
038#endregion
039
040#region 八进制转化
041publicstaticlongEightToTen(stringnumber)
042{
043returnConverHexToTen(number, 8);
044}
045#endregion
046
047#region 十进制转化
048/// <summary>
049/// 十进制到二进制
050/// </summary>
051/// <param name="number"></param>
052/// <returns></returns>
053publicstaticstringTenToTwo(longnumber)
054{
055returnConverHex(number, 2);
056}
057
058/// <summary>
059/// 十进制到八进制
060/// </summary>
061/// <param name="number"></param>
062/// <returns></returns>
063publicstaticstringTenToEight(longnumber)
064{
065returnConverHex(number, 8);
066}
067
068/// <summary>
069/// 十进制到16进制
070/// </summary>
071/// <param name="number"></param>
072/// <returns></returns>
073publicstaticstringTenToSixteen(longnumber)
074{
075returnConverHex(number, 16);
076}
077
078/// <summary>
079/// 十进制到32进制
080/// </summary>
081/// <param name="number"></param>
082/// <returns></returns>
083publicstaticstringTenTo32(longnumber)
084{
085returnConverHex(number, 32);
086}
087#endregion
088
089#region 16进制转化
090publicstaticlongSixteenToTen(stringnumber)
091{
092returnConverHexToTen(number, 16);
093}
094#endregion
095
096#region 32进制转化
097publicstaticlongSixToTen(stringnumber)
098{
099returnConverHexToTen(number, 32);
100}
101#endregion
102
103#region 公用方法
104/// <summary>
105/// 获取相应的字符,16进制与32进制需要使用
106/// </summary>
107/// <param name="num"></param>
108/// <returns></returns>
109privatestaticstringSixFont(longnum)
110{
111if(num < 10)
112returnnum + "";
113returnThreeSix.Substring((int)num - 10, 1);
114}
115
116/// <summary>
117/// 获取相应bit的数量,便于转化
118/// </summary>
119/// <param name="str"></param>
120/// <param name="size"></param>
121/// <returns></returns>
122privatestaticstring[] GetByte(stringstr, intsize)
123{
124intlenght = str.Length % size;
125
126if(lenght > 0)
127str.PadLeft((str.Length / size + 1) * size, '0');
128string[] strs = newstring[str.Length / size];
129for(inti = 0; i < str.Length / size; i++)
130{
131strs[i] = str.Substring(i * size, (i + 1) * size);
132}
133returnstrs;
134}
135
136/// <summary>
137/// 10进制转化成其他进制
138/// </summary>
139/// <param name="number">需转换字符</param>
140/// <param name="format">转化到;如转换到2进制,这里键入2</param>
141/// <returns></returns>
142publicstaticstringConverHex(longnumber, intformat)
143{
144if(number == 0)
145returnnumber + "";
146StringBuilder str = newStringBuilder();
147while(number > 0)
148{
149str.Insert(0, SixFont((number % format)));
150number /= format;
151}
152returnstr.ToString();
153}
154
155/// <summary>
156/// 其他进制转化到10进制
157/// </summary>
158/// <param name="num">需转换字符</param>
159/// <param name="format">转化到;如转换到2进制,这里键入2</param>
160/// <returns></returns>
161publicstaticlongConverHexToTen(stringnum, intformat)
162{
163longallCount = 0;
164for(inti = 0; i < num.Length; i++)
165{
166strings = num.Substring(i, 1);
167intn = 0;
168if(!int.TryParse(s, outn)) {
169n= GetNumberByStr(s);
170}
171
172if(n < 0)
173thrownewException("输入字符有误,请确保该字符真实存在");
174
175if(n > 0)
176{
177longcount = 1;
178for(intj = 0; j < num.Length - i - 1; j++)
179{
180count *= format;
181}
182count *= n;
183allCount += count;
184}
185
186}
187returnallCount;
188}
189
190/// <summary>
191/// 根据字符,获取对应的十进制数据
192/// </summary>
193/// <param name="c"></param>
194/// <returns></returns>
195privatestaticInt32 GetNumberByStr(stringc)
196{
197inti = ThreeSix.IndexOf(c);
198if(i < 0)
199returni;
200returni + 10;
201}
202
203#endregion
204
205}
206}