110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox3.Text = Convert(int.Parse(textBox1.Text), int.Parse(textBox2.Text));
}
private string Convert(int n, int Base)
{
bool gt0 = true;
if (Base < 10 || Base > 36) return null;
if (n == 0) return "0";
if (n < 0)
{
n = -n;
gt0 = false;
}
StringBuilder sb = new StringBuilder();
string t = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while (n > 0)
{
sb.Append(t[n % Base]);
n /= Base;
}
t = sb.ToString();
n = t.Length;
sb.Length = 0;
if (!gt0) sb.Append("-");
while (--n >= 0) sb.Append(t[n]);
return sb.ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AnyConvertx2x
{
/// <summary>
/// 数字进制转换类
/// </summary>
public class AnyConvert
{
private static char[] rDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'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' };
/// <summary>
/// 将指定基数的数字的 System.String 表示形式转换为等效的 64 位有符号整数。
/// </summary>
/// <param name="value">包含数字的 System.String。</param>
/// <param name="fromBase">value 中数字的基数,它必须是[2,36]</param>
/// <returns>等效于 value 中的数字的 64 位有符号整数。- 或 - 如果 value 为 null,则为零。</returns>
private long x2h(string value, int fromBase)
{
value = value.Trim();
if (string.IsNullOrEmpty(value))
{
return 0L;
}
string sDigits = new string(rDigits, 0, fromBase);
long result = 0;
//value = reverse(value).ToUpper(); 1
value = value.ToUpper();// 2
for (int i = 0; i < value.Length; i++)
{
if (!sDigits.Contains(value[i].ToString()))
{
throw new ArgumentException(string.Format("The argument \"{0}\" is not in {1} system.", value[i], fromBase));
}
else
{
try
{
//result += (long)Math.Pow(fromBase, i) * getcharindex(rDigits, value[i]); 1
result += (long)Math.Pow(fromBase, i) * getcharindex(rDigits, value[value.Length - i - 1]);// 2
}
catch
{
throw new OverflowException("运算溢出.");
}
}
}
return result;
}
private int getcharindex(char[] arr, char value)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == value)
{
return i;
}
}
return 0;
}
//long转化为toBase进制
private string h2x(long value, int toBase)
{
int digitIndex = 0;
long longPositive = Math.Abs(value);
int radix = toBase;
char[] outDigits = new char[63];
for (digitIndex = 0; digitIndex <= 64; digitIndex++)
{
if (longPositive == 0) { break; }
outDigits[outDigits.Length - digitIndex - 1] =
rDigits[longPositive % radix];
longPositive /= radix;
}
return new string(outDigits, outDigits.Length - digitIndex, digitIndex);
}
/// <summary>
/// 任意进制转换,将fromBase进制表示的value转换为toBase进制
/// </summary>
/// <param name="value"></param>
/// <param name="fromBase">原来进制(2-36进制)</param>
/// <param name="toBase">转换后的进制(2-36进制)</param>
/// <returns></returns>
public string X2X(string value, int fromBase, int toBase)
{
if (string.IsNullOrEmpty(value.Trim()))
{
return string.Empty;
}
if (fromBase < 2 || fromBase > 36)
{
throw new ArgumentException(String.Format("The fromBase radix \"{0}\" is not in the range 2..36.", fromBase));
}
if (toBase < 2 || toBase > 36)
{
throw new ArgumentException(String.Format("The toBase radix \"{0}\" is not in the range 2..36.", toBase));
}
long m = x2h(value, fromBase);
string r = h2x(m, toBase);
return r;
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnyConvertx2x;
namespace AnyRadixConvert
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AnyConvert convert = new AnyConvert();
textBox2 .Text = convert.X2X(textBox1 .Text , 36, 10);//36 To 10
}
private void button2_Click(object sender, EventArgs e)
{
AnyConvert convert = new AnyConvert();
textBox2.Text = convert.X2X(textBox1.Text, 10, 36);//10 To 36
}
}
}
string dic = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = value.Aggregate(0L, (sum, c) => sum * 36 + dic.IndexOf(c));
private int ConvertToInt32(char c)
{
if (c >= 48 && c <= 57) { return c - 48; }
if (c >= 97 && c <= 122) { return c - 87; }
if (c >= 65 && c <= 90) { return c - 55; }
return 0;
}
public int ConvertToInt(string v, int fromBase)
{
char[] chars = v.ToCharArray();
Array.Reverse(chars);
int fb = 1;
int value = 0;
foreach(char aa in chars)
{
value += ConvertToInt32(aa) * fb;
fb *= fromBase;
}
return value;
}
public string ConvertToString(int v, int fromBase)
{
if (fromBase < 2 || fromBase > 36) throw new ArgumentException();
List<char> cs = new List<char>(36);
while (v > 0)
{
int x = v % fromBase;
int c = 48;
if (x > 10) { c = 87; }
cs.Add((char)(x + c));
v /= fromBase;
}
cs.Reverse();
return new string(cs.ToArray());
}