隨手一揮,記錄一下。 以下代碼,包含了需求有: 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,大佬勿噴。