不用系统函数,怎么编写将十进制转成十六进制与十六进制转成十进制的函数?请写代码? 我作毕设那会儿都傻傻的作过这种转换10->16就是用除法了16->10就用switch乘以权了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用堆栈实现了10转16,你看看吧namespace Stack{ /// <summary> /// 栈结点类 /// </summary> public class StackNode { /// <summary> /// 栈的内容 /// </summary> public string Data; /// <summary> /// 下一个指针 /// </summary> public StackNode Next; /// <summary> /// 构造函数 /// </summary> public StackNode(string MyData) { Data=MyData; } } /// <summary> /// 栈类 /// </summary> public class MyStack { private StackNode Base; //栈底 private StackNode Top; //栈顶 private int StackLen; //栈长 /// <summary> /// 初始化栈 /// </summary> public MyStack() { Base=null; Top=null; StackLen=0; } /// <summary> /// 清除栈 /// </summary> public void ClearStack() { Top=Base; StackLen=0; return; } /// <summary> /// 判断一个栈是否为空 /// </summary> public bool StackEmpty() { if(StackLen==0) return true; return false; } /// <summary> /// 取栈的长度 /// </summary> public int Length { get{return StackLen;} } /// <summary> /// 返回栈顶元素 /// </summary> public string GetTop() { string result; if(!StackEmpty()) { result=Top.Data; } else { result=""; } return result; } /// <summary> /// 入栈操作 /// </summary> public void Push(string e) { StackNode NewNode=new StackNode(e); if(StackLen==0) { Base=NewNode; Top=Base; } if(Top==Base) { NewNode.Next=Base; Top=NewNode; } else{ NewNode.Next=Top; Top=NewNode; } StackLen++; return; } /// <summary> /// 出栈操作 /// </summary> public string Pop() { string result=""; if(StackLen!=0) { result=Top.Data; Top=Top.Next; StackLen--; } return result; } /// <summary> /// 遍历全栈 /// </summary> public string PrintStack() { string result=""; StackNode move; if(StackLen!=0) { move=Top; while(move!=Base) { result=result + move.Data; move=move.Next; } } return result; } }}实现算法的函数: private string E10to16(int num) { MyStack Conversion =new MyStack(); int yushu; string result=""; while(num!=0) { yushu=num % 8; Conversion.Push(yushu.ToString()); num=num/8; } while(!Conversion.StackEmpty()) { result=result + Conversion.Pop() + ","; } return result; } 老实说,实在是因为100分太有诱惑力了!我真的想要分!其实我对C#也是初学者,不得不花时间了解了一些字符串操作和动态数组方面的东西,用了一个小时弄出来了。比VB复杂多了!下面的是全部代码,方法一定很笨,我还是初学者,但我试验多次,没有问题。把它保存为Form1.cs,执行一下试试吧。能给我分吧?using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace Convert16and10{ //相互转换的类 public class ConvertClass { //十六进制转换为十进制 public int Convert16to10(string hexNumber) { int showby10=0; //单个的十六进制转换成的十进制 int tmpresult=0; //临时结果 //循环带入的十六进制参数 for (int i=0;i<hexNumber.Length;i++) { //转换为大写 switch (hexNumber.Substring(i,1).ToUpper()) { case "0": showby10 = 0; break; case "1": showby10 = 1; break; case "2": showby10 = 2; break; case "3": showby10 = 3; break; case "4": showby10 = 4; break; case "5": showby10 = 5; break; case "6": showby10 = 6; break; case "7": showby10 = 7; break; case "8": showby10 = 8; break; case "9": showby10 = 9; break; case "A": showby10 = 10; break; case "B": showby10 = 11; break; case "C": showby10 = 12; break; case "D": showby10 = 13; break; case "E": showby10 = 14; break; case "F": showby10 = 15; break; } //switch (hexNumber.Substring(i,1).ToUpper()) //计算结果 tmpresult = tmpresult + showby10 * (int)Math.Pow(16,(hexNumber.Length)-i-1); } //for (int i=0;i<hexNumber.Length;i++) return tmpresult; } //public int Convert16to10(string hexNumber) //十进制转换为十六进制 public string Convert10to16(int decimalNumber) { int tmpint=decimalNumber; //被16除之后得到的整数 ArrayList tmpresultarray = new ArrayList(); //十六进制结果的临时数组 string tmpstring=""; //将十六进制的每一位转换为字符串 string tmpresult=""; //临时结果 //循环带入的十进制参数,得到十六进制的每一位 while (tmpint >= 16) { //整数除以16之后的余数,即十六进制最后一位的值,带入临时数组。用十进制表示 tmpresultarray.Add(tmpint % 16); //将整数除以16,得到新的整数 tmpint = (tmpint - tmpint % 16) / 16; } //向数组添加十六进制的第一位 tmpresultarray.Add(tmpint); //循环数组 for (int i=tmpresultarray.Count;i>0;i--) { switch ((int)tmpresultarray[i-1]) { case 0: tmpstring = "0"; break; case 1: tmpstring = "1"; break; case 2: tmpstring = "2"; break; case 3: tmpstring = "3"; break; case 4: tmpstring = "4"; break; case 5: tmpstring = "5"; break; case 6: tmpstring = "6"; break; case 7: tmpstring = "7"; break; case 8: tmpstring = "8"; break; case 9: tmpstring = "9"; break; case 10: tmpstring = "A"; break; case 11: tmpstring = "B"; break; case 12: tmpstring = "C"; break; case 13: tmpstring = "D"; break; case 14: tmpstring = "E"; break; case 15: tmpstring = "F"; break; } //switch ((int)tmpresultarray[i]) //计算结果 tmpresult = tmpresult + tmpstring; } //for (int i=tmpresultarray.Count-1;i>0;i--) //得到结果 return tmpresult; } //public string Convert10to16(int decimalNumber) } //public class ConvertClass****下面还有一半***** public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBox2 = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.textBox4 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // label1 // this.label1.Location = new System.Drawing.Point(16, 16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(96, 16); this.label1.TabIndex = 1; this.label1.Text = "目标十六进制"; // // label2 // this.label2.Location = new System.Drawing.Point(16, 48); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(88, 16); this.label2.TabIndex = 3; this.label2.Text = "结果"; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(112, 40); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(128, 21); this.textBox2.TabIndex = 2; this.textBox2.Text = "textBox2"; // // label3 // this.label3.Location = new System.Drawing.Point(24, 144); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(80, 16); this.label3.TabIndex = 7; this.label3.Text = "结果"; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(112, 112); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(128, 21); this.textBox3.TabIndex = 6; this.textBox3.Text = "textBox3"; // // label4 // this.label4.Location = new System.Drawing.Point(24, 120); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(80, 16); this.label4.TabIndex = 5; this.label4.Text = "目标十进制"; // // textBox4 // this.textBox4.Location = new System.Drawing.Point(112, 144); this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(128, 21); this.textBox4.TabIndex = 4; this.textBox4.Text = "textBox4"; // // button1 // this.button1.Location = new System.Drawing.Point(120, 72); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 24); this.button1.TabIndex = 8; this.button1.Text = "16-10"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Location = new System.Drawing.Point(112, 176); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(96, 24); this.button2.TabIndex = 9; this.button2.Text = "10-16"; this.button2.Click += new System.EventHandler(this.button2_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(112, 16); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(128, 21); this.textBox1.TabIndex = 11; this.textBox1.Text = "textBox1"; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(264, 213); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1, this.button2, this.button1, this.label3, this.textBox3, this.label4, this.textBox4, this.label2, this.textBox2, this.label1}); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } //十六进制转换为十进制 private void button1_Click(object sender, System.EventArgs e) { ConvertClass MyConvert = new ConvertClass(); textBox2.Text=MyConvert.Convert16to10(textBox1.Text).ToString(); } //十进制转换为十六进制 private void button2_Click(object sender, System.EventArgs e) { ConvertClass MyConvert = new ConvertClass(); textBox4.Text=MyConvert.Convert10to16(int.Parse(textBox3.Text)).ToString(); } }}能给我分吧?我的专家分太少了。 我没有加任何错误处理的代码,自己看懂之后加上吧,在每个switch的default中加入。 我的网站上有这道题,包含超级详细的注释,你可以看看。http://mullen.go.163.com/Littlesoft/articles/CSTest_5.htm8.15前有效。其实大家的思路差不多,因此我也没有细看上面的解答。你都看看吧,也许谁的技巧上有什么特点。 C#中socket队列 winform for循环 C# VS2008安装部署项目失败 。。。救命。。。vs2005winform项目显示不了视图啦 winform 动态生成控件,取值和赋值问题。 读取数据库中表的具体某行我某列? 在C#里可否直接把字符串转换为byte数组? 求Xceed Grid 3.0的注册码。(100分)是3.0的!!! ?菜鸟问个关于时间的小问题 如何从checkedboxlist的被选中项中获得该项名称 hello world 运行不到,没安装FrameworkSDK的机器就运行不到了吗? 重写Exception类出现的问题
namespace Stack
{
/// <summary>
/// 栈结点类
/// </summary>
public class StackNode
{
/// <summary>
/// 栈的内容
/// </summary>
public string Data;
/// <summary>
/// 下一个指针
/// </summary>
public StackNode Next;
/// <summary>
/// 构造函数
/// </summary>
public StackNode(string MyData)
{
Data=MyData;
}
}
/// <summary>
/// 栈类
/// </summary>
public class MyStack
{
private StackNode Base; //栈底
private StackNode Top; //栈顶
private int StackLen; //栈长
/// <summary>
/// 初始化栈
/// </summary>
public MyStack()
{
Base=null;
Top=null;
StackLen=0;
}
/// <summary>
/// 清除栈
/// </summary>
public void ClearStack()
{
Top=Base;
StackLen=0;
return;
}
/// <summary>
/// 判断一个栈是否为空
/// </summary>
public bool StackEmpty()
{
if(StackLen==0)
return true;
return false;
}
/// <summary>
/// 取栈的长度
/// </summary>
public int Length
{
get{return StackLen;}
}
/// <summary>
/// 返回栈顶元素
/// </summary>
public string GetTop()
{
string result;
if(!StackEmpty())
{
result=Top.Data;
}
else
{
result="";
}
return result;
}
/// <summary>
/// 入栈操作
/// </summary>
public void Push(string e)
{
StackNode NewNode=new StackNode(e);
if(StackLen==0)
{
Base=NewNode;
Top=Base;
}
if(Top==Base)
{
NewNode.Next=Base;
Top=NewNode;
}
else{
NewNode.Next=Top;
Top=NewNode;
}
StackLen++;
return;
}
/// <summary>
/// 出栈操作
/// </summary>
public string Pop()
{
string result="";
if(StackLen!=0)
{
result=Top.Data;
Top=Top.Next;
StackLen--;
}
return result;
}
/// <summary>
/// 遍历全栈
/// </summary>
public string PrintStack()
{
string result="";
StackNode move;
if(StackLen!=0)
{
move=Top;
while(move!=Base)
{
result=result + move.Data;
move=move.Next;
} }
return result;
} }}实现算法的函数:
private string E10to16(int num)
{
MyStack Conversion =new MyStack();
int yushu;
string result="";
while(num!=0)
{
yushu=num % 8;
Conversion.Push(yushu.ToString());
num=num/8;
}
while(!Conversion.StackEmpty())
{
result=result + Conversion.Pop() + ",";
}
return result;
}
其实我对C#也是初学者,不得不花时间了解了一些字符串操作和动态数组方面的东西,用了一个小时弄出来了。比VB复杂多了!
下面的是全部代码,方法一定很笨,我还是初学者,但我试验多次,没有问题。把它保存为Form1.cs,执行一下试试吧。能给我分吧?
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Convert16and10
{
//相互转换的类
public class ConvertClass
{
//十六进制转换为十进制
public int Convert16to10(string hexNumber)
{
int showby10=0; //单个的十六进制转换成的十进制
int tmpresult=0; //临时结果 //循环带入的十六进制参数
for (int i=0;i<hexNumber.Length;i++)
{
//转换为大写
switch (hexNumber.Substring(i,1).ToUpper())
{
case "0":
showby10 = 0;
break;
case "1":
showby10 = 1;
break;
case "2":
showby10 = 2;
break;
case "3":
showby10 = 3;
break;
case "4":
showby10 = 4;
break;
case "5":
showby10 = 5;
break;
case "6":
showby10 = 6;
break;
case "7":
showby10 = 7;
break;
case "8":
showby10 = 8;
break;
case "9":
showby10 = 9;
break;
case "A":
showby10 = 10;
break;
case "B":
showby10 = 11;
break;
case "C":
showby10 = 12;
break;
case "D":
showby10 = 13;
break;
case "E":
showby10 = 14;
break;
case "F":
showby10 = 15;
break;
} //switch (hexNumber.Substring(i,1).ToUpper()) //计算结果
tmpresult = tmpresult + showby10 * (int)Math.Pow(16,(hexNumber.Length)-i-1); } //for (int i=0;i<hexNumber.Length;i++) return tmpresult;
} //public int Convert16to10(string hexNumber)
//十进制转换为十六进制
public string Convert10to16(int decimalNumber)
{
int tmpint=decimalNumber; //被16除之后得到的整数
ArrayList tmpresultarray = new ArrayList(); //十六进制结果的临时数组
string tmpstring=""; //将十六进制的每一位转换为字符串
string tmpresult=""; //临时结果
//循环带入的十进制参数,得到十六进制的每一位
while (tmpint >= 16)
{
//整数除以16之后的余数,即十六进制最后一位的值,带入临时数组。用十进制表示
tmpresultarray.Add(tmpint % 16);
//将整数除以16,得到新的整数
tmpint = (tmpint - tmpint % 16) / 16;
}
//向数组添加十六进制的第一位
tmpresultarray.Add(tmpint);
//循环数组
for (int i=tmpresultarray.Count;i>0;i--)
{
switch ((int)tmpresultarray[i-1])
{
case 0:
tmpstring = "0";
break;
case 1:
tmpstring = "1";
break;
case 2:
tmpstring = "2";
break;
case 3:
tmpstring = "3";
break;
case 4:
tmpstring = "4";
break;
case 5:
tmpstring = "5";
break;
case 6:
tmpstring = "6";
break;
case 7:
tmpstring = "7";
break;
case 8:
tmpstring = "8";
break;
case 9:
tmpstring = "9";
break;
case 10:
tmpstring = "A";
break;
case 11:
tmpstring = "B";
break;
case 12:
tmpstring = "C";
break;
case 13:
tmpstring = "D";
break;
case 14:
tmpstring = "E";
break;
case 15:
tmpstring = "F";
break;
} //switch ((int)tmpresultarray[i])
//计算结果
tmpresult = tmpresult + tmpstring; } //for (int i=tmpresultarray.Count-1;i>0;i--) //得到结果
return tmpresult;
} //public string Convert10to16(int decimalNumber) } //public class ConvertClass****下面还有一半*****
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1; private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
} protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(96, 16);
this.label1.TabIndex = 1;
this.label1.Text = "目标十六进制";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 48);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 16);
this.label2.TabIndex = 3;
this.label2.Text = "结果";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(112, 40);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(128, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "textBox2";
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 144);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(80, 16);
this.label3.TabIndex = 7;
this.label3.Text = "结果";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(112, 112);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(128, 21);
this.textBox3.TabIndex = 6;
this.textBox3.Text = "textBox3";
//
// label4
//
this.label4.Location = new System.Drawing.Point(24, 120);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(80, 16);
this.label4.TabIndex = 5;
this.label4.Text = "目标十进制";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(112, 144);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(128, 21);
this.textBox4.TabIndex = 4;
this.textBox4.Text = "textBox4";
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 72);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 24);
this.button1.TabIndex = 8;
this.button1.Text = "16-10";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 176);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 24);
this.button2.TabIndex = 9;
this.button2.Text = "10-16";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(112, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 11;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(264, 213);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button2,
this.button1,
this.label3,
this.textBox3,
this.label4,
this.textBox4,
this.label2,
this.textBox2,
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion [STAThread]
static void Main()
{
Application.Run(new Form1());
} //十六进制转换为十进制
private void button1_Click(object sender, System.EventArgs e)
{
ConvertClass MyConvert = new ConvertClass();
textBox2.Text=MyConvert.Convert16to10(textBox1.Text).ToString();
} //十进制转换为十六进制
private void button2_Click(object sender, System.EventArgs e)
{
ConvertClass MyConvert = new ConvertClass();
textBox4.Text=MyConvert.Convert10to16(int.Parse(textBox3.Text)).ToString();
}
}
}能给我分吧?我的专家分太少了。
http://mullen.go.163.com/Littlesoft/articles/CSTest_5.htm
8.15前有效。
其实大家的思路差不多,因此我也没有细看上面的解答。你都看看吧,也许谁的技巧上有什么特点。