Exemplo n.º 1
0
        private void BtnHexToAscii_Click(object sender, EventArgs e)
        {
            rtbOutput.Text = string.Empty;
            if (string.IsNullOrEmpty(rtbInput.Text))
            {
                return;
            }
            btnHexToAscii.Enabled = false;

            var content = rtbInput.Text;

            _ = Task.Run(async() =>
            {
                try
                {
                    string str = Encoding.ASCII.GetString(StringByteUtils.StringToBytes(content));
                    await Task.Factory.FromAsync(BeginInvoke(new Action(() =>
                    {
                        rtbOutput.AppendText(str);
                    })), EndInvoke);
                }
                catch
                {
                    MessageBox.Show("请正确输入需要转换的内容");
                }
                finally
                {
                    await Task.Factory.FromAsync(BeginInvoke(new Action(() =>
                    {
                        btnHexToAscii.Enabled = true;
                    })), EndInvoke);
                }
            });
        }
Exemplo n.º 2
0
        private void btnCrc_Click(object sender, EventArgs e)
        {
            var value = tbIn.Text;

            byte[] data = cbIsAscii.Checked ? Encoding.Default.GetBytes(value) : StringByteUtils.StringToBytes(value);

            tbCrc16.Text     = StringByteUtils.BytesToString(CRC.Crc16(data, data.Length));
            tbCrcc.Text      = StringByteUtils.BytesToString(CRC.CRC16_C(data));
            tbUpdateCRC.Text = StringByteUtils.BytesToString(StringByteUtils.GetBytes(CRC.UpdateCRC(data, data.Length), true));
            tbGBcrc16.Text   = $"##{value.Length.ToString().PadLeft(4, '0')}{value}{StringByteUtils.BytesToString(CRC.GBcrc16(data, data.Length)).Replace(" ", "")}";
            tbHBcrc16.Text   = $"##{value.Length.ToString().PadLeft(4, '0')}{value}{StringByteUtils.BytesToString(CRC.HBcrc16(data, data.Length)).Replace(" ", "")}";
        }
Exemplo n.º 3
0
 /// <summary>
 /// 2字节或4字节转换为整数
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void MS_ToInt_Click(object sender, EventArgs e)
 {
     byte[] IntByte = StringByteUtils.StringToBytes(txtData.SelectedText);
     if (IntByte.Length == 2)
     {
         MessageBox.Show(BitConverter.ToInt16(IntByte, 0).ToString(), "整数值");
     }
     else
     {
         MessageBox.Show(BitConverter.ToInt32(IntByte, 0).ToString(), "整数值");
     }
 }
Exemplo n.º 4
0
        private void BtnHexToFloat_Click(object sender, EventArgs e)
        {
            rtbOutput.Text = string.Empty;
            if (string.IsNullOrEmpty(rtbInput.Text))
            {
                return;
            }
            btnHexToFloat.Enabled = false;

            string[] splitStr = rtbInput.Text.Split('\n');

            _ = Task.Run(async() =>
            {
                var stringBuilder = new StringBuilder();
                for (int j = 0; j < splitStr.Length; j++)
                {
                    try
                    {
                        byte[] Data = StringByteUtils.StringToBytes(splitStr[j]);
                        if (Data.Length % 4 != 0)
                        {
                            MessageBox.Show("请正确输入数据!(每一行字节数必须是4的倍数)[第" + (j + 1) + "行]");
                        }
                        for (int i = 0; i < Data.Length / 4; i++)
                        {
                            if (cbHB.Checked)
                            {
                                byte[] temp = new byte[4];
                                Array.Copy(Data, i * 4, temp, 0, 4);
                                Array.Reverse(temp);
                                stringBuilder.Append(BitConverter.ToSingle(temp, 0).ToString() + tbSplit.Text);
                            }
                            else
                            {
                                stringBuilder.Append(BitConverter.ToSingle(Data, i * 4).ToString() + tbSplit.Text);
                            }
                        }
                    }
                    catch
                    {
                        MessageBox.Show("请正确输入需要转换的内容![第" + (j + 1) + "行]");
                    }
                    stringBuilder.Append('\n');
                }
                await Task.Factory.FromAsync(BeginInvoke(new Action(() =>
                {
                    rtbOutput.AppendText(stringBuilder.ToString());
                    btnHexToFloat.Enabled = true;
                })), EndInvoke);
            });
        }
Exemplo n.º 5
0
 private async void dataGridView1_CellEndEditAsync(object sender, DataGridViewCellEventArgs e)
 {
     if (!string.IsNullOrEmpty(dataGridView1.Rows[e.RowIndex].Cells["key"].Value?.ToString()))
     {
         try
         {
             var key   = StringByteUtils.StringToBytes(dataGridView1.Rows[e.RowIndex].Cells["key"].Value.ToString() !);
             var value = StringByteUtils.StringToBytes(dataGridView1.Rows[e.RowIndex].Cells["value"].Value?.ToString() ?? "");
             if (int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["delay"].Value?.ToString() ?? "0", out var delay))
             {
                 await Global.AutoReplyConfig !.AddOrUpdateAsync(key, value, delay);
             }
             else
             {
                 await Global.AutoReplyConfig !.AddOrUpdateAsync(key, value);
             }
         }
         catch (Exception)
         {
         }
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 8字节转换为双精度浮点数
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void MS_ToDouble_Click(object sender, EventArgs e)
 {
     byte[] IntByte = StringByteUtils.StringToBytes(txtData.SelectedText);
     MessageBox.Show(BitConverter.ToDouble(IntByte, 0).ToString(), "双精度浮点数值");
 }