当前位置: 首页>JAVA>正文

任意進制轉換方法,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>

https://www.nshth.com/java/326403.html
>

相关文章:

  • 任意進制轉換方法
  • 進制轉換器含過程
  • XAML的功能
  • 補碼一位乘法booth算法
  • 進制轉換器帶過程
  • 進制轉換的代碼
  • 10110100補碼轉換為原碼
  • 進制轉換編程
  • pdf去水印軟件免費版,java批量去除pdf簽名,刪除簽名圖標
  • java多線程面試題及答案,JAVA8線程池THREADPOOLEXECUTOR底層原理及其源碼解析
  • java編程,java.lang.Class:是反射的源頭
  • java基礎面試題及答案,HTML CSS 基礎 面試題
  • java編寫軟件工具,Xson:Java對象序列化和反序列化工具
  • nlp預訓練模型,NLP-D62-nlp比賽D31刷題D15
  • kafka如何使用,kafka javax.management.InstanceAlreadyExistsException: kafka.consumer:
  • ssm畢設項目企業部門報銷管理g9d62(java+VUE+Mybatis+Maven+Mysql+sprnig)
  • java小游戲合集,java 煙花_Java 美麗的煙花
  • table列合并,poi操作excel之列合并
  • 找不到指定模塊怎么辦,在烏版圖安裝軟件包時候報錯:E:無法定位軟件包
  • 學云計算好就業嗎,對不起,云計算技術又走錯路了
  • 數電模電基礎知識總結,數電模電實驗課程
  • java的基礎知識,「JavaSE」-面向對象
  • 擴展內存,Java編程內存分析簡要
  • java多線程面試題及答案,【階段一】java之面向對象上
  • java 工作流框架,Activiti工作流使用之SpringBoot整合Activiti
  • 模型的應用形態包括哪些,模型設計準則
  • c語言程序設計培訓班南寧,南寧從零開始學習編程
  • 服務器,Spring Security oAuth2創建認證服務器模塊
  • Java jdk14.0.1安裝簡單步驟
  • 安裝ug12.0當前頁面的腳本發生錯誤,ug提示找不到html文件,[圖文教程] 以下文件無法加載,導致打開操作失敗: 使用當前搜索選項找不到文件,部件已卸載.
  • java執行cmd命令找不到指定文件,java編譯找不到文件_解決cmd運行java程序“找不到文件”提示的方案
  • 線上學畫畫的機構排名,拍樂云推出業內首個「線上美術教學音視頻方案」,打造極致互動體驗
  • day18-java
  • 取兩者中較小值函數,求兩個數中的較大值
  • java多線程面試題及答案,python中的多任務-多線程和多進程
  • 關于Arthas如何遠程監視Java程序
  • Java8 Stream流中的 collect() 方法,遠比你想象中的強大
  • 劉德華《天若有情》,天若有情