﻿{"id":738,"date":"2013-06-19T01:02:31","date_gmt":"2013-06-18T17:02:31","guid":{"rendered":"http:\/\/www.tansoo.cn\/?p=738"},"modified":"2014-09-11T01:05:15","modified_gmt":"2014-09-10T17:05:15","slug":"%e4%b8%a4%e7%a7%8dcrc%e7%ae%97%e6%b3%95%e7%9a%84delphi%e6%ba%90%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/www.tansoo.cn\/?p=738","title":{"rendered":"\u4e24\u79cdCRC\u7b97\u6cd5\u7684Delphi\u6e90\u4ee3\u7801"},"content":{"rendered":"<p>\u5171\u6709\u4e24\u79cd\u7b97\u6cd5\u3002\u4e0b\u9762\u8fd9\u4e2a\u7eaf\u8ba1\u7b97\u51fa\u7684\uff1a <\/p>\n<pre class=\"lang:delphi decode:true \" >function CalCRC8005(AData:array of Byte; AStart,AEnd:Integer): Word;\r\nconst\r\n  GENP = $8005;\r\n\r\nvar\r\n  crc:Word;\r\n  i:Integer;\r\n\r\nprocedure CalOneByte(AByte:Byte);\r\nvar\r\n  j:Integer;\r\n  msb:Word;   \/\/\u79fb\u51fa\u7684\u6700\u9ad8\u4f4d\r\n  tmp:Word;\r\nbegin\r\n  tmp:=AByte;\r\n  tmp:=tmp shl 8;\r\n  crc:=crc xor tmp;\r\n\r\n  for j := 0 to 7 do\r\n  begin\r\n    msb:= crc and $8000;\r\n    crc:=crc shl 1;\r\n    if msb = $8000 then\r\n      crc:= crc xor GENP;\r\n  end;\r\nend;\r\n\r\nbegin\r\n  crc:=$FFFF;\r\n  \/\/crc:=$0000;\r\n  for i := AStart to AEnd do\r\n    CalOneByte(AData[i]);\r\n  Result:=crc;\r\nend;<\/pre>\n<p><!--more--><\/p>\n<p>\u4e0b\u9762\u662f\u53e6\u4e00\u79cd\u67e5\u8868\u6cd5\uff0c\u7ed3\u679c\u4e0e\u7b2c\u4e00\u79cd\u4e00\u6837 <\/p>\n<pre class=\"lang:delphi decode:true \" >type\r\n  TForm1 = class(TForm)\r\n    Button1: TButton;\r\n    Label1: TLabel;\r\n    procedure Button1Click(Sender: TObject);\r\n  private\r\n    { Private declarations }\r\n  public\r\n    { Public declarations }\r\n  end;\r\n\r\nconst\r\n  CrcTable: array [0 .. 255] of word = ($0000, $8005, $800F, $000A, $801B,\r\n    $001E, $0014, $8011, $8033, $0036, $003C, $8039, $0028, $802D, $8027, $0022,\r\n    $8063, $0066, $006C, $8069, $0078, $807D, $8077, $0072, $0050, $8055, $805F,\r\n    $005A, $804B, $004E, $0044, $8041, $80C3, $00C6, $00CC, $80C9, $00D8, $80DD,\r\n    $80D7, $00D2, $00F0, $80F5, $80FF, $00FA, $80EB, $00EE, $00E4, $80E1, $00A0,\r\n    $80A5, $80AF, $00AA, $80BB, $00BE, $00B4, $80B1, $8093, $0096, $009C, $8099,\r\n    $0088, $808D, $8087, $0082, $8183, $0186, $018C, $8189, $0198, $819D, $8197,\r\n    $0192, $01B0, $81B5, $81BF, $01BA, $81AB, $01AE, $01A4, $81A1, $01E0, $81E5,\r\n    $81EF, $01EA, $81FB, $01FE, $01F4, $81F1, $81D3, $01D6, $01DC, $81D9, $01C8,\r\n    $81CD, $81C7, $01C2, $0140, $8145, $814F, $014A, $815B, $015E, $0154, $8151,\r\n    $8173, $0176, $017C, $8179, $0168, $816D, $8167, $0162, $8123, $0126, $012C,\r\n    $8129, $0138, $813D, $8137, $0132, $0110, $8115, $811F, $011A, $810B, $010E,\r\n    $0104, $8101, $8303, $0306, $030C, $8309, $0318, $831D, $8317, $0312, $0330,\r\n    $8335, $833F, $033A, $832B, $032E, $0324, $8321, $0360, $8365, $836F, $036A,\r\n    $837B, $037E, $0374, $8371, $8353, $0356, $035C, $8359, $0348, $834D, $8347,\r\n    $0342, $03C0, $83C5, $83CF, $03CA, $83DB, $03DE, $03D4, $83D1, $83F3, $03F6,\r\n    $03FC, $83F9, $03E8, $83ED, $83E7, $03E2, $83A3, $03A6, $03AC, $83A9, $03B8,\r\n    $83BD, $83B7, $03B2, $0390, $8395, $839F, $039A, $838B, $038E, $0384, $8381,\r\n    $0280, $8285, $828F, $028A, $829B, $029E, $0294, $8291, $82B3, $02B6, $02BC,\r\n    $82B9, $02A8, $82AD, $82A7, $02A2, $82E3, $02E6, $02EC, $82E9, $02F8, $82FD,\r\n    $82F7, $02F2, $02D0, $82D5, $82DF, $02DA, $82CB, $02CE, $02C4, $82C1, $8243,\r\n    $0246, $024C, $8249, $0258, $825D, $8257, $0252, $0270, $8275, $827F, $027A,\r\n    $826B, $026E, $0264, $8261, $0220, $8225, $822F, $022A, $823B, $023E, $0234,\r\n    $8231, $8213, $0216, $021C, $8219, $0208, $820D, $8207, $0202);\r\n\r\nvar\r\n  Form1: TForm1;\r\n  CrcL, CrcH: byte;\r\n\r\nimplementation\r\n\r\n{$R *.dfm}\r\n\r\nprocedure ResetCrc;\r\nbegin\r\n  CrcL := $FF;\r\n  CrcH := $FF;\r\nend;\r\n\r\nprocedure UpdateCrc(const num: byte);\r\nvar\r\n  table_addr: byte;\r\nbegin\r\n  table_addr := (num xor CrcH);\r\n  CrcH := (CrcTable[table_addr] shr 8) xor CrcL;\r\n  CrcL := (CrcTable[table_addr] and $00FF);\r\nend;\r\n\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nvar\r\n  i0, s_len: integer;\r\n  WrBuf: array [1 .. 32] of byte;\r\n  s:string;\r\nbegin\r\n  s:='';\r\n  WrBuf[1] := $7F;\r\n  WrBuf[2] := $80;\r\n  WrBuf[3] := $01;\r\n  WrBuf[4] := $07;\r\n  \/\/WrBuf[5] := $ef;\r\n  \/\/WrBuf[6] := $00;\r\n  ResetCrc;\r\n  for i0 := 2 to 4 do\r\n    UpdateCrc(WrBuf[i0]); \/\/ \u8ba1\u7b97 Crc\uff0c0x7f \u4e0d\u53c2\u52a0 Crc \u8ba1\u7b97\r\n  WrBuf[i0] := CrcL;\r\n  inc(i0);\r\n  WrBuf[i0] := CrcH;\r\n  s_len := i0;\r\n  for i0 := 1 to s_len do s:=s+inttohex(WrBuf[i0],2)+' ';\r\n  label1.Caption := s;\r\nend;\r\nend.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5171\u6709\u4e24\u79cd\u7b97\u6cd5\u3002\u4e0b\u9762\u8fd9\u4e2a\u7eaf\u8ba1\u7b97\u51fa\u7684\uff1a function CalCRC8005(AData:array of By [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[15,14],"class_list":["post-738","post","type-post","status-publish","format-standard","hentry","category-program","tag-crc","tag-delphi"],"_links":{"self":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=738"}],"version-history":[{"count":0,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=\/wp\/v2\/posts\/738\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tansoo.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}