进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串

  • 进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串已关闭评论
  • 29 次浏览
  • A+
所属分类:.NET技术
摘要

随手一挥,记录一下。以下代码,包含了需求有:  1.十进制转二进制。  2.二进制转16进制

随手一挥,记录一下。

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

  1.十进制转二进制。

  2.二进制转16进制

  3.将十进制转二进制的位数,不足5位的  用0 补齐5位。

  4.将转换后的二进制拼接起来,截取8位 作转换为16进制,最后不足8位的,用 “1111” 补齐。在作转换 。 

不废话,上代码先。 

主界面图片:

进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串

 

主要后台代码:

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>

用到的其他类

进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串

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

进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串进制转换,十进制 转换为2进制,再由二进制转换为16进制,截取8位二进制组合字符串

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,大佬勿喷。