進位轉換,十進位 轉換為2進位,再由二進位轉換為16進位,截取8位二進位組合字元串

来源:https://www.cnblogs.com/Nicqx/archive/2022/05/17/16281490.html
-Advertisement-
Play Games

隨手一揮,記錄一下。 以下代碼,包含了需求有: 1.十進位轉二進位。 2.二進位轉16進位 3.將十進位轉二進位的位數,不足5位的 用0 補齊5位。 4.將轉換後的二進位拼接起來,截取8位 作轉換為16進位,最後不足8位的,用 “1111” 補齊。在作轉換 。 不廢話,上代碼先。 主界面圖片: 主要 ...


隨手一揮,記錄一下。

以下代碼,包含了需求有:

  1.十進位轉二進位。

  2.二進位轉16進位

  3.將十進位轉二進位的位數,不足5位的  用0 補齊5位。

  4.將轉換後的二進位拼接起來,截取8位 作轉換為16進位,最後不足8位的,用 “1111” 補齊。在作轉換 。 

不廢話,上代碼先。 

主界面圖片:

 

主要後臺代碼:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AisgV2ConfigurationFile
{
    /// <summary>
    /// ConvertAdd.xaml 的交互邏輯
    /// </summary>
    public partial class ConvertAdd : UserControl
    {
        List<TextBox> textBoxes = new List<TextBox>();
        public ConvertAdd()
        {
            InitializeComponent();
            var elements = container.Children;
            foreach (UIElement element in elements)
            {
                if (element is TextBox)
                {
                    var box = (TextBox)element;
                    if (box.Name.StartsWith("textBox"))
                    {
                        textBoxes.Add(box);
                    }
                }
            }
            textBoxes.Sort((x, y) =>
            {
                var result1 = Regex.Match(x.Name, @"\d+$").Value.ToInt32();
                var result2 = Regex.Match(y.Name, @"\d+$").Value.ToInt32();
                return result1 - result2;
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = 0; i < textBoxes.Count; i++)
                {
                    if (string.IsNullOrWhiteSpace(textBoxes[i].Text))
                    {
                        //textBoxes[i].Text = DefaultNumber;
                        continue;
                    }
                    var value = Convert.ToInt64($"{textBoxes[i].Text}", 10);
                    var svalue = Convert.ToString(value, 2);
                    svalue = InspectionLength(svalue, 5);
                    textBoxes[i].Text = svalue;

                }

                List<string> bit8List = new List<string>();
                StringBuilder sb = new StringBuilder();
                foreach (var box in textBoxes)
                {
                    sb.Append(box.Text + " ");
                }
                tbConsole.Text = sb.ToString();

                
                var bit8 = sb.ToString().Replace(" ", "");
                for (int i = 0; i < bit8.Length / 8; i++)
                {
                    bit8List.Add(bit8.Substring(i * 8, 8));
                }
                if (bit8List.Count != 100)
                {
                    bit8List.Add(bit8.Substring(bit8.Length-4, 4) + "1111");
                }
                MessageBox.Show(bit8List.Count.ToString());
                tbConsole1.Text = string.Join(" ", bit8List);

                byte[] by = new byte[bit8List.Count];
                for (int i = 0; i < bit8List.Count; i++)
                {
                    by[i] = Convert.ToByte(bit8List[i], 2);

                    tbConsole2.Text += by[i].ToString("X2") + " ";
                }
                GlobalVariable.fileValueByte.AddRange(by);
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message) ;
            }
            #region
            //// 文本文件追加
            //using(StreamWriter sw = File.AppendText($"{Directory.GetCurrentDirectory()}\\text.txt"))
            //{
            //    sw.WriteLine(tbConsole2.Text);
            //    sw.Flush();
            //}

            // 數據流追加
            //using (FileStream fileStream = new FileStream($"{Directory.GetCurrentDirectory()}\\text.bin", FileMode.Append, FileAccess.Write))
            //{
            //    fileStream.Write(by, 0, by.Length);
            //    fileStream.Flush();
            //}
            #endregion
        }

        /// <summary>
        /// 檢驗長度
        /// </summary>
        /// <param name="value">原始值</param>
        /// <param name="retain">保留幾位</param>
        /// <returns></returns>
        private string InspectionLength(string value, int retain)
        {
            if (value.Length < retain)
            {
                int length = retain - value.Length;
                string ZeroFilling = "";
                for (int i = 0; i < length; i++)
                {
                    ZeroFilling += "0";
                }
                value = ZeroFilling + value;
            }

            return value;
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            for (int i = 0; i < textBoxes.Count; i++)
            {
                //textBoxes[i].Text = $"{i}";
                textBoxes[i].Text = "31";//重置textbox預設值為31
                
            }
            //for (int i = 0; i < textBoxes.Count; i++)
            //{
            //    textBoxes[i].Text = "";
            //}
            tbConsole.Text = "";
            tbConsole1.Text = "";
            tbConsole2.Text = "";

        }
    }
}

 

前臺代碼

    <Grid x:Name="container">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Text="31" Grid.Column="0" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox1"></TextBox>
        <TextBox Grid.Row="0" Text="31" Grid.Column="1" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox2"></TextBox>
        <TextBox Grid.Row="0" Text="31" Grid.Column="2" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox3"></TextBox>
        <TextBox Grid.Row="0" Text="31" Grid.Column="3" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox4"></TextBox>
        <TextBox Grid.Row="0" Text="31" Grid.Column="4" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox5"></TextBox>
        <TextBox Grid.Row="1" Text="31" Grid.Column="0" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox6"></TextBox>
        <TextBox Grid.Row="1" Text="31" Grid.Column="1" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox7"></TextBox>
        <TextBox Grid.Row="1" Text="31" Grid.Column="2" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox8"></TextBox>
        <TextBox Grid.Row="1" Text="31" Grid.Column="3" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox9"></TextBox>
        <TextBox Grid.Row="1" Text="31" Grid.Column="4" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox10"></TextBox>

        <TextBox Grid.Row="2" Text="31" Grid.Column="0" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox11"></TextBox>
        <TextBox Grid.Row="2" Text="31" Grid.Column="1" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox12"></TextBox>
        <TextBox Grid.Row="2" Text="31" Grid.Column="2" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox13"></TextBox>
        <TextBox Grid.Row="2" Text="31" Grid.Column="3" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox14"></TextBox>
        <TextBox Grid.Row="2" Text="31" Grid.Column="4" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox15"></TextBox>

        <TextBox Grid.Row="3" Text="31" Grid.Column="0" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox16"></TextBox>
        <TextBox Grid.Row="3" Text="31" Grid.Column="1" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox17"></TextBox>
        <TextBox Grid.Row="3" Text="31" Grid.Column="2" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox18"></TextBox>
        <TextBox Grid.Row="3" Text="31" Grid.Column="3" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox19"></TextBox>
        <TextBox Grid.Row="3" Text="31" Grid.Column="4" VerticalContentAlignment="Center" Padding="50 0 0 0" x:Name="textBox20"></TextBox>
        <TextBox Grid.Row="4"   Grid.ColumnSpan="5" VerticalContentAlignment="Center" TextAlignment="Center" x:Name="tbConsole"></TextBox>

        <TextBox Grid.Row="5"  Grid.ColumnSpan="5" VerticalContentAlignment="Center" TextAlignment="Center" x:Name="tbConsole1"></TextBox>

        <TextBox Grid.Row="6"  Grid.ColumnSpan="5" VerticalContentAlignment="Center" TextAlignment="Center" x:Name="tbConsole2"></TextBox>

        <Button Grid.Row="7" Grid.Column="2" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Click="Button_Click">轉換</Button>
        <Button Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Center" Padding="2" Click="Button_Click_1">重置</Button>
       
    </Grid>

用到的其他類

1     public class GlobalVariable
2     {
3         /// <summary>
4         /// 全局共用變數,存放16進位數值。ConvertAddNumber
5         /// </summary>
6         public static string FileValue = string.Empty;
7         public static List<byte> fileValueByte = new List<byte>();
8     }
View Code
1    public static class NumberExtensions
2     {
3         public static int ToInt32(this string str)
4         {
5             return Convert.ToInt32(str);
6         }
7     }
View Code

 

以上僅僅位自己練習demo,大佬勿噴。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • “==”和equals的區別 首先我們應該知道的是: “==”是運算符,如果是基本數據類型,則比較存儲的值;如果是引用數據類型,則比較所指向對象的地址值。 equals是Object的方法,比較的是所指向的對象的地址值,一般情況下,重寫之後比較的是對象的值。 一、對象類型不同 1、equals(): ...
  • 把destoon數據生成json,一般用於百度小程式、QQ小程式和微信小程式或者原生APP,由於系統是GB2312編碼,所以服務端編寫的時候我們進行了一些編碼轉換的處理,保證服務端訪問的編碼是UTF-8就可以。不多了,下麵乾貨來了。如果你是程式或此段代碼對你有幫助,希望收藏!! 代碼來了,在根目錄新 ...
  • 環境介紹: python 3.8 解釋器 pycharm 2021專業版 >>> 激活碼 編輯器 谷歌瀏覽器 谷歌驅動 selenium >>> 驅動 >>> 瀏覽器 模塊使用: 採集一個視頻 requests >>> pip install requests re 採集多個視頻 selenium ...
  • 1引言:這裡主要做三件事 1.1resources文件夾下創建spring-mvc.xml並配置:開啟註解驅動(mvc:annotation-driven),靜態資源過濾(mvc:default-servlet-handler),視圖解析器(InternalResourceViewResolver) ...
  • 在 JVM 進行垃圾回收之前,我們需要先判斷一個對象是否存活,判斷對象是否存活採用了兩種方法: 引用計數法 給對象中添加一個引用計數器,每引用這個對象一次,計數器 +1,當引用失效時,計數器 -1。當引用計數器為 0 時,則表示該對象可被回收。 Java 不適用原因:無法解決對象互相迴圈引用的問題 ...
  • 大家好,這篇文章分享了C程式設計(譚浩強)第五版第四章課後題答案,所有程式已經測試能夠正常運行,如果小伙伴發現有錯誤的的地方,歡迎留言告訴我,我會及時改正!感謝大家的觀看!!! ...
  • 大家好,這篇文章分享了C程式設計(譚浩強)第五版第三章課後題答案,所有程式已經測試能夠正常運行,如果小伙伴發現有錯誤的的地方,歡迎留言告訴我,我會及時改正!感謝大家的觀看!!! ...
  • NLog 是我們在 .NET 領域使用非常廣泛的日誌組件。它預設使用 xml 來維護它的配置。最近有幾個同學問我當使用 AgileConfig 的時候如何配置 NLog 。因為 AgileConfig 不支持集成 xml 格式的配置。其實 NLog 是支持從 appsettings.json / I ...
一周排行
    -Advertisement-
    Play Games
  • 概述:在C#中,++i和i++都是自增運算符,其中++i先增加值再返回,而i++先返回值再增加。應用場景根據需求選擇,首碼適合先增後用,尾碼適合先用後增。詳細示例提供清晰的代碼演示這兩者的操作時機和實際應用。 在C#中,++i 和 i++ 都是自增運算符,但它們在操作上有細微的差異,主要體現在操作的 ...
  • 上次發佈了:Taurus.MVC 性能壓力測試(ap 壓測 和 linux 下wrk 壓測):.NET Core 版本,今天計劃準備壓測一下 .NET 版本,來測試並記錄一下 Taurus.MVC 框架在 .NET 版本的性能,以便後續持續優化改進。 為了方便對比,本文章的電腦環境和測試思路,儘量和... ...
  • .NET WebAPI作為一種構建RESTful服務的強大工具,為開發者提供了便捷的方式來定義、處理HTTP請求並返迴響應。在設計API介面時,正確地接收和解析客戶端發送的數據至關重要。.NET WebAPI提供了一系列特性,如[FromRoute]、[FromQuery]和[FromBody],用 ...
  • 原因:我之所以想做這個項目,是因為在之前查找關於C#/WPF相關資料時,我發現講解圖像濾鏡的資源非常稀缺。此外,我註意到許多現有的開源庫主要基於CPU進行圖像渲染。這種方式在處理大量圖像時,會導致CPU的渲染負擔過重。因此,我將在下文中介紹如何通過GPU渲染來有效實現圖像的各種濾鏡效果。 生成的效果 ...
  • 引言 上一章我們介紹了在xUnit單元測試中用xUnit.DependencyInject來使用依賴註入,上一章我們的Sample.Repository倉儲層有一個批量註入的介面沒有做單元測試,今天用這個示例來演示一下如何用Bogus創建模擬數據 ,和 EFCore 的種子數據生成 Bogus 的優 ...
  • 一、前言 在自己的項目中,涉及到實時心率曲線的繪製,項目上的曲線繪製,一般很難找到能直接用的第三方庫,而且有些還是定製化的功能,所以還是自己繪製比較方便。很多人一聽到自己畫就害怕,感覺很難,今天就分享一個完整的實時心率數據繪製心率曲線圖的例子;之前的博客也分享給DrawingVisual繪製曲線的方 ...
  • 如果你在自定義的 Main 方法中直接使用 App 類並啟動應用程式,但發現 App.xaml 中定義的資源沒有被正確載入,那麼問題可能在於如何正確配置 App.xaml 與你的 App 類的交互。 確保 App.xaml 文件中的 x:Class 屬性正確指向你的 App 類。這樣,當你創建 Ap ...
  • 一:背景 1. 講故事 上個月有個朋友在微信上找到我,說他們的軟體在客戶那邊隔幾天就要崩潰一次,一直都沒有找到原因,讓我幫忙看下怎麼回事,確實工控類的軟體環境複雜難搞,朋友手上有一個崩潰的dump,剛好丟給我來分析一下。 二:WinDbg分析 1. 程式為什麼會崩潰 windbg 有一個厲害之處在於 ...
  • 前言 .NET生態中有許多依賴註入容器。在大多數情況下,微軟提供的內置容器在易用性和性能方面都非常優秀。外加ASP.NET Core預設使用內置容器,使用很方便。 但是筆者在使用中一直有一個頭疼的問題:服務工廠無法提供請求的服務類型相關的信息。這在一般情況下並沒有影響,但是內置容器支持註冊開放泛型服 ...
  • 一、前言 在項目開發過程中,DataGrid是經常使用到的一個數據展示控制項,而通常表格的最後一列是作為操作列存在,比如會有編輯、刪除等功能按鈕。但WPF的原始DataGrid中,預設只支持固定左側列,這跟大家習慣性操作列放最後不符,今天就來介紹一種簡單的方式實現固定右側列。(這裡的實現方式參考的大佬 ...