資源簡介
目前在做和GPS通訊協議相關的項目項目中有用到異或和校驗的部分就自己寫了小工具包含源碼給需要的朋友。 輸入內容為 16進制字符串 輸出也是16進制的字符串

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?CRC
{
????public?partial?class?FormCRC?:?Form
????{
????????public?FormCRC()
????????{
????????????InitializeComponent();
????????}
????????private?void?FormCRC_Load(object?sender?EventArgs?e)
????????{
????????}
????????///?
????????///?CRC異或校驗?add?by?heiyanfei?2013-06-19
????????///?
????????///?命令字符串
????????///?
????????private?static?string?CRC(string?cmdString)
????????{
????????????try
????????????{
????????????????//CRC寄存器
????????????????int?CRCCode?=?0;
????????????????//將字符串拆分成為16進制字節數據然后兩位兩位進行異或校驗
????????????????for?(int?i?=?1;?i?????????????????{
????????????????????string?cmdHex?=?cmdString.Substring(i?*?2?2);
????????????????????if?(i?==?1)
????????????????????{
????????????????????????string?cmdPrvHex?=?cmdString.Substring((i?-?1)?*?2?2);
????????????????????????CRCCode?=?(byte)Convert.ToInt32(cmdPrvHex?16)?^?(byte)Convert.ToInt32(cmdHex?16);
????????????????????}
????????????????????else
????????????????????{
????????????????????????CRCCode?=?(byte)CRCCode?^?(byte)Convert.ToInt32(cmdHex?16);
????????????????????}
????????????????}
????????????????return?Convert.ToString(CRCCode?16).ToUpper();//返回16進制校驗碼
????????????}
????????????catch
????????????{
????????????????throw;
????????????}
????????}
????????private?void?btnConvert_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????txtCRCCode.Text?=?CRC(txtCMDString.Text.Replace(“?“?““));
????????????}
????????????catch
????????????{
????????????????MessageBox.Show(“校驗失敗請檢查字符串是否包含特殊字符“);
????????????}
????????}
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????9216??2013-06-19?18:51??CRC\CRC\bin\Debug\CRC.exe
?????文件??????24064??2013-06-19?18:51??CRC\CRC\bin\Debug\CRC.pdb
?????文件??????11608??2013-06-19?21:10??CRC\CRC\bin\Debug\CRC.vshost.exe
?????文件???????3568??2013-06-19?18:44??CRC\CRC\CRC.csproj
?????文件???????2048??2013-06-19?18:51??CRC\CRC\FormCRC.cs
?????文件???????5170??2013-06-19?18:51??CRC\CRC\FormCRC.Designer.cs
?????文件???????5817??2013-06-19?18:51??CRC\CRC\FormCRC.resx
?????文件????????466??2013-06-19?21:10??CRC\CRC\obj\x86\Debug\CRC.csproj.FileListAbsolute.txt
?????文件???????9216??2013-06-19?18:51??CRC\CRC\obj\x86\Debug\CRC.exe
?????文件????????180??2013-06-19?18:51??CRC\CRC\obj\x86\Debug\CRC.FormCRC.resources
?????文件??????24064??2013-06-19?18:51??CRC\CRC\obj\x86\Debug\CRC.pdb
?????文件????????180??2013-06-19?18:44??CRC\CRC\obj\x86\Debug\CRC.Properties.Resources.resources
?????文件???????6023??2013-06-19?18:51??CRC\CRC\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件???????4088??2013-06-19?18:51??CRC\CRC\obj\x86\Debug\ResGen.read.1.tlog
?????文件????????602??2013-06-19?18:51??CRC\CRC\obj\x86\Debug\ResGen.write.1.tlog
?????文件????????486??2013-06-19?18:26??CRC\CRC\Program.cs
?????文件???????1356??2013-06-19?18:26??CRC\CRC\Properties\AssemblyInfo.cs
?????文件???????2856??2013-06-19?18:26??CRC\CRC\Properties\Resources.Designer.cs
?????文件???????5612??2013-06-19?18:26??CRC\CRC\Properties\Resources.resx
?????文件???????1088??2013-06-19?18:26??CRC\CRC\Properties\Settings.Designer.cs
?????文件????????249??2013-06-19?18:26??CRC\CRC\Properties\Settings.settings
?????文件????????851??2013-06-19?18:26??CRC\CRC.sln
????..A..H.?????10240??2013-06-19?18:43??CRC\CRC.suo
?????目錄??????????0??2013-06-19?18:26??CRC\CRC\obj\x86\Debug\TempPE
?????目錄??????????0??2013-06-19?18:51??CRC\CRC\obj\x86\Debug
?????目錄??????????0??2013-06-19?18:44??CRC\CRC\bin\Debug
?????目錄??????????0??2013-06-19?18:26??CRC\CRC\bin\Release
?????目錄??????????0??2013-06-19?18:26??CRC\CRC\obj\x86
?????目錄??????????0??2013-06-19?18:26??CRC\CRC\bin
?????目錄??????????0??2013-06-19?18:26??CRC\CRC\obj
............此處省略6個文件信息
評論
共有 條評論