示例#1
0
        private string CheckCode(string str)
        {
            long   result = (long)CRC32Helper.GetCRC32Str(str);
            string code   = CRC32_PREFIX + Convert.ToString(result, 16).ToUpper().PadLeft(8, '0');

            return(code);
        }
示例#2
0
        /// <summary>
        /// 读Flash完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void readSpiBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            startAddress = 0;
            //判断是否出错
            if (e.Error != null)
            {
                MessageBoxHelper.ShowError(e.Error.Message);
                return;
            }
            //判断用户是否取消了操作
            if (e.Cancelled)
            {
                this.fileTransmitInfoLabel.Text = Resources.ForceProgramDialog_CancelTransmit;
                return;
            }

            this.fileTransmitInfoLabel.Text = Resources.ForceProgramDialog_ReadSpiCompleted;


            if (e.Result != null)
            {
                spiDataCRC32 = CRC32Helper.CalculateCRC32(e.Result.ToString());
                if (spiDataCRC32 != fileCRC32)
                {
                    MessageBoxHelper.ShowError(Resources.ForceProgramDialog_CRC32Failed);
                }
                else
                {
                    MessageBoxHelper.ShowInfo(Resources.ForceProgramDialog_CRC32Success);
                }
            }

            this.startCompareButton.Enabled = true;
            this.stopTransmitButton.Enabled = false;
        }
        public static string GetUniqueString()
        {
            //24位唯一编码
            //string str = CRC32Helper.GetCRC32(Guid.NewGuid().ToString()) + CRC32Helper.GetCRC32(DateTime.Now.ToString()) + CRC32Helper.GetCRC32(r.Next(99999999).ToString());
            //if (str.Length < 24)  //包含长度小于24位的结果,调整到24位。
            //    str = GetUniqueString().PadRight(24, str[0]);
            //16位唯一编码
            string str = CRC32Helper.GetCRC32(Guid.NewGuid().ToString()) + CRC32Helper.GetCRC32((DateTime.Now.Ticks - r.Next(99999999)).ToString());

            if (str.Length < 16)  //包含长度小于16位的结果,调整到16位。
            {
                str = GetUniqueString().PadRight(16, str[0]);
            }
            return(str);
        }
示例#4
0
        /// <summary>
        /// 打开bin文件显示在TextBox中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void openFilebutton_Click(object sender, EventArgs e)
        {
            //显示打开文件对话框
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.fileNameTextBox.Text = openFileDialog1.FileName;
                this.checkSumlabel.Text   = string.Format("{0:X8}", CRC32Helper.GetFileCRC32(openFileDialog1.FileName));

                fileCRC32 = CRC32Helper.GetFileCRC32(this.fileNameTextBox.Text);

                using (FileStream stream = File.Open(openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    BinaryReader binaryReader = new BinaryReader(stream);
                    byte[]       src          = binaryReader.ReadBytes((int)stream.Length);

                    StringBuilder builder = new StringBuilder();

                    for (uint i = 0; i < src.Length; i += 16)
                    {
                        builder.AppendFormat("{0:X8}:  ", i);
                        for (uint j = 0; j < 16; j++)
                        {
                            builder.AppendFormat("{0:X2}", src[i + j]);
                            if (j < 15)
                            {
                                builder.Append(" ");
                            }
                        }
                        if (i < src.Length - 16)
                        {
                            builder.Append("\r\n");
                        }
                    }
                    this.binSourceFileTextBox.Text = builder.ToString();
                }
            }
        }
        public void GetCRC32StringTest()
        {
            uint result = CRC32Helper.CalculateCRC32("123456789");

            Assert.AreEqual(result, 0xCBF43926u);
        }
        public void GetCRC32ByteTest()
        {
            uint result = CRC32Helper.CalculateCRC32(new byte[] { 0x68, 0xFF, 0xFF, 0xAB, 0x13, 0x26 });

            Assert.AreEqual(result, 0x27CB9B07u);
        }