任意進制轉換方法,Xamarin實現一個進制轉換器
任意進制轉換方法,Xamarin實現一個進制轉換器
先上圖吧!
任意進制轉換方法、
代碼實現其實很簡單:
先創建一個Transform類(用來作為為進制轉換的工具)
using System;
namespace Conversion
{
? ? class Transform
? ? {
? ? ? ? internal string TenToBinary(long value)//將十進制轉換為二進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(value, 2);
? ? ? ? }
? ? ? ? internal string TenToEight(long value)//將十進制轉換為八進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(value, 8);
? ? ? ? }
? ? ? ? internal string TenToSixteen(long value)//將十進制轉換為十六進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(value, 16);
? ? ? ? }
? ? ? ? internal string BinaryToEight(long value)//將二進制轉換為八進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(
? ? ? ? ? ? ? ? Convert.ToInt64(value.ToString(), 2), 8);
? ? ? ? }
? ? ? ? internal string BinaryToTen(long value)//將二進制轉換為十進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToInt64(
? ? ? ? ? ? ? ? value.ToString(), 2).ToString();
? ? ? ? }
? ? ? ? internal string BinaryToSixteen(long value)//將二進制轉換為十六進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(
? ? ? ? ? ? ? ? Convert.ToInt64(value.ToString(), 2), 16);
? ? ? ? }
? ? ? ? internal string EightToBinary(long value)//將八進制轉換為二進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(
? ? ? ? ? ? ? ? Convert.ToInt64(value.ToString(), 8), 2);
? ? ? ? }
? ? ? ? internal string EightToTen(long value)//將八進制轉換為十進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToInt64(
? ? ? ? ? ? ? ? value.ToString(), 8).ToString();
? ? ? ? }
? ? ? ? internal string EightToSixteen(long value)//將八進制轉換為十六進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(
? ? ? ? ? ? ? ? Convert.ToInt64(value.ToString(), 8), 16);
? ? ? ? }
? ? ? ? internal string SixteenToBinary(string value)//將十六進制轉換為二進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(
? ? ? ? ? ? ? ? Convert.ToInt64(value, 16), 2);
? ? ? ? }
? ? ? ? internal string SixteenToEight(string value)//將十六進制轉換為八進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToString(
? ? ? ? ? ? ? ? Convert.ToInt64(value, 16), 8);
? ? ? ? }
? ? ? ? internal string SixteenToTen(string value)//將十六進制轉換為十進制
? ? ? ? {
? ? ? ? ? ? return Convert.ToUInt64(value, 16).ToString();
? ? ? ? }
? ? }
}
?
然后在MainActivity中實現功能:
using Android.App;
using Android.Widget;
using Android.OS;
using System;
namespace Conversion
{
? ? [Activity(Label = "Conversion", MainLauncher = true,Icon ="@drawable/book")]
? ? public class MainActivity : Activity
? ? {
? ? ? ? Button btnTransform;
? ? ? ? private EditText edtInput,edtOutput;
? ? ? ? RadioButton twoC,eightC,sixteenC, twoC2, eightC2, sixteenC2,tenC,tenC2;
? ? ? ? Spinner spwhere;
? ? ? ? protected override void OnCreate(Bundle savedInstanceState)
? ? ? ? {
? ? ? ? ? ? base.OnCreate(savedInstanceState);
? ? ? ? ? ? // Set our view from the "main" layout resource
? ? ? ? ? ? SetContentView(Resource.Layout.Main);
? ? ? ? ? ? edtInput = FindViewById<EditText>(Resource.Id.edtinput);
? ? ? ? ? ? edtOutput = FindViewById<EditText>(Resource.Id.edtoutput);
? ? ? ? ? ? btnTransform = FindViewById<Button>(Resource.Id.btnTransform);
? ? ? ? ? ? twoC = FindViewById<RadioButton>(Resource.Id.twoC);
? ? ? ? ? ? eightC = FindViewById<RadioButton>(Resource.Id.eightC);
? ? ? ? ? ? sixteenC = FindViewById<RadioButton>(Resource.Id.sixteenC);
? ? ? ? ? ? twoC2 = FindViewById<RadioButton>(Resource.Id.twoC2);
? ? ? ? ? ? eightC2 = FindViewById<RadioButton>(Resource.Id.eightC2);
? ? ? ? ? ? sixteenC2 = FindViewById<RadioButton>(Resource.Id.sixteenC2);
? ? ? ? ? ? tenC = FindViewById<RadioButton>(Resource.Id.tenC);
? ? ? ? ? ? tenC2 = FindViewById<RadioButton>(Resource.Id.tenC2);
? ? ? ? ? ? var comments = FindViewById<EditText>(Resource.Id.edtinput);
? ? ? ? ? ? var comments2 = FindViewById<EditText>(Resource.Id.edtoutput);
? ? ? ? ? ? //選擇條
? ? ? ? ?
? ? ? ? ? ? btnTransform.Click += ButtenTransform;
? ? ? ? ? ?
? ? ? ? ? ? comments.Click += delegate
? ? ? ? ? ? {
? ? ? ? ? ? ? ? comments.Text = "";
? ? ? ? ? ? };
? ? ? ? ? ? comments2.Click += delegate
? ? ? ? ? ? {
? ? ? ? ? ? ? ? comments2.Text = "";
? ? ? ? ? ? };
? ? ? ? }
? ? ? ? private void ButtenTransform(object sender,EventArgs args)
? ? ? ? {
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Action();//調用Action方法進行轉換操作
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Toast.MakeText(this, "輸入錯誤請重試,錯誤", ToastLength.Short).Show();
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 進制轉換
? ? ? ? /// </summary>
? ? ? ? private void Action()
? ? ? ? {
? ? ? ? ? ? if (!sixteenC.Checked)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? long p_line_value;
? ? ? ? ? ? ? ? if (long.TryParse(edtInput.Text, out p_line_value))//判斷輸入的是否是正確的賦值
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (tenC.Checked)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (tenC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = edtInput.Text;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (twoC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().TenToBinary(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (eightC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().TenToEight(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if(sixteenC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().TenToSixteen(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.MakeText(this, "輸入錯誤,重試!", ToastLength.Short).Show();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else if (twoC.Checked)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (tenC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().BinaryToTen(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (twoC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = edtInput.Text;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (eightC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().BinaryToEight(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (sixteenC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().BinaryToSixteen(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.MakeText(this, "輸入錯誤,重試!", ToastLength.Short).Show();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else if(eightC.Checked)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? if (tenC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().EightToTen(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (twoC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().EightToBinary(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (eightC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = edtInput.Text;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else if (sixteenC2.Checked)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().EightToSixteen(long.Parse(edtInput.Text));
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? Toast.MakeText(this, "輸入錯誤,重試!", ToastLength.Short).Show();
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? } ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (tenC2.Checked)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().SixteenToTen(edtInput.Text);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (twoC2.Checked)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().SixteenToBinary(edtInput.Text);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (eightC2.Checked)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? edtOutput.Text = new Transform().SixteenToEight(edtInput.Text);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if (sixteenC2.Checked)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? edtOutput.Text = edtInput.Text;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Toast.MakeText(this, "輸入錯誤,重試!", ToastLength.Short).Show();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
布局代碼送上:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:orientation="vertical"
? ? android:layout_width="fill_parent"
? ? android:layout_height="fill_parent">
? ? <LinearLayout
? ? ? ? android:orientation="horizontal"
? ? ? ? android:minWidth="25px"
? ? ? ? android:minHeight="80px"
? ? ? ? android:layout_marginTop="20dp"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/linearLayoutForName">
? ? ? ? <TextView
? ? ? ? ? ? android:text="輸入:"
? ? ? ? ? ? android:layout_width="81.5dp"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:id="@+id/textViewNme"
? ? ? ? ? ? android:textAllCaps="true"
? ? ? ? ? ? android:textSize="20dp"
? ? ? ? ? ? android:gravity="center" />
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="291.0dp"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:id="@+id/edtinput" />
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:orientation="horizontal"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/linearLayout1"
? ? ? ? android:paddingTop="10dp"
? ? ? ? android:paddingEnd="20dp"
? ? ? ? android:gravity="center">
? ? ? ? <RadioGroup
? ? ? ? ? ? android:minWidth="25px"
? ? ? ? ? ? android:minHeight="25px"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:id="@+id/radioGroup1"
? ? ? ? ? ? android:orientation="horizontal">
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="十進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/tenC" />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:checked="true"
? ? ? ? ? ? ? ? android:text="二進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/twoC" />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="八進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/eightC" />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="十六進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/sixteenC" />
? ? ? ? </RadioGroup>
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:orientation="horizontal"
? ? ? ? android:minWidth="25px"
? ? ? ? android:minHeight="25px"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/linearLayout2">
? ? ? ? <TextView
? ? ? ? ? ? android:text="輸出:"
? ? ? ? ? ? android:layout_width="81.5dp"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:id="@+id/textViewName"
? ? ? ? ? ? android:textAllCaps="true"
? ? ? ? ? ? android:textSize="20dp"
? ? ? ? ? ? android:gravity="center" />
? ? ? ? <EditText
? ? ? ? ? ? android:layout_width="291.0dp"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:id="@+id/edtoutput" />
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? android:orientation="horizontal"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:id="@+id/linearLayout2"
? ? ? ? android:paddingTop="10dp"
? ? ? ? android:paddingEnd="20dp"
? ? ? ? android:gravity="center">
? ? ? ? <RadioGroup
? ? ? ? ? ? android:minWidth="25px"
? ? ? ? ? ? android:minHeight="25px"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:id="@+id/radioGroup2"
? ? ? ? ? ? android:orientation="horizontal">
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="十進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/tenC2" />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:checked="true"
? ? ? ? ? ? ? ? android:text="二進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/twoC2" />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="八進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/eightC2" />
? ? ? ? ? ? <RadioButton
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:text="十六進制"
? ? ? ? ? ? ? ? android:textSize="15dp"
? ? ? ? ? ? ? ? android:id="@+id/sixteenC2" />
? ? ? ? </RadioGroup>
? ? </LinearLayout>
? ? <Button
? ? ? ? android:text="轉 ?換"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="75.0dp"
? ? ? ? android:id="@+id/btnTransform"
? ? ? ? android:textSize="25dp"
? ? ? ? android:layout_marginTop="13.0dp" />
</LinearLayout>