.NetFramework2.0中提供了进制之间进行转化的方法:
Convert.ToString(int value,int i)
//从十进制转化成其他进制
//value即需要转化的十进制数;i目标进制
Convert.ToInt32(string value,int i)
//从其他进制转化成十进制
//value即需要转化的其他进制的数,i进制数
具体代码如下:Button1事件比较啰唆,也比较笨;Button2事件推荐使用
aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>进制转换</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="转化" />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="2">二进制</asp:ListItem>
<asp:ListItem Value="8">八进制</asp:ListItem>
<asp:ListItem Value="10">十进制</asp:ListItem>
<asp:ListItem Value="16">十六进制</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="2">二进制</asp:ListItem>
<asp:ListItem Value="8">八进制</asp:ListItem>
<asp:ListItem Value="10">十进制</asp:ListItem>
<asp:ListItem Value="16">十六进制</asp:ListItem>
</asp:DropDownList>
转换前<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
转换后<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>
</form>
</body>
</html>
cs页面文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == DropDownList2.SelectedValue)
{
TextBox2.Text = TextBox1.Text;
}
else
{
switch (DropDownList1.SelectedValue)
{
case "2":
if (DropDownList2.SelectedValue == "10")
{
TextBox2.Text = Convert.ToInt32(TextBox1.Text, 2).ToString();
}
if (DropDownList2.SelectedValue == "8")//二进制转化八进制:先转化成十进制在转化成8进制
{
TextBox2.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 2), 8);
}
if (DropDownList2.SelectedValue == "16")
{
TextBox2.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 2), 16);
}
break;
case "10":
if (DropDownList2.SelectedValue == "2")
{
TextBox2.Text = Convert.ToString(int.Parse(TextBox1.Text), 2);
}
if (DropDownList2.SelectedValue == "8")
{
TextBox2.Text = Convert.ToString(int.Parse(TextBox1.Text), 8);
}
if (DropDownList2.SelectedValue == "16")
{
TextBox2.Text = Convert.ToString(int.Parse(TextBox1.Text), 16);
}
break;
case "8":
if (DropDownList2.SelectedValue == "2")
{
TextBox2.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text,10), 2);
}
if (DropDownList2.SelectedValue == "10")
{
TextBox2.Text = Convert.ToInt32(TextBox1.Text, 8).ToString();
}
if (DropDownList2.SelectedValue == "16")
{
TextBox2.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 10), 16);
}
break;
case "16":
if (DropDownList2.SelectedValue == "2")
{
TextBox2.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 10), 2);
}
if (DropDownList2.SelectedValue == "10")
{
TextBox2.Text = Convert.ToInt32(TextBox1.Text, 16).ToString();
}
if (DropDownList2.SelectedValue == "8")
{
TextBox2.Text = Convert.ToString(Convert.ToInt32(TextBox1.Text, 10), 8);
}
break;
default: TextBox2.Text = TextBox1.Text; break;
}
}
}
/// <summary>
/// 轻松实现进制之间的相互转换
/// </summary>
/// <param name="value">初始值</param>
/// <param name="fromBase">原来的进制</param>
/// <param name="toBase">新的进制</param>
/// <returns>转化后的值</returns>
public string ConvertString(string value, int fromBase, int toBase)
{
try
{
int intValue = Convert.ToInt32(value, fromBase);
return Convert.ToString(intValue, toBase);
}
catch (Exception ex)
{
return ex.Message;
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox2.Text = ConvertString(TextBox1.Text, int.Parse(DropDownList1.SelectedValue), int.Parse(DropDownList2.SelectedValue));
}
}