您的位置:程序门 -> delphi -> 网络通信/分布式开发



求用delphi编写的lrc校验位算法函数,急!!!


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


求用delphi编写的lrc校验位算法函数,急!!!
发表于:2008-01-10 09:50:37 楼主
  某命令串为":010200000001fc"
  其16进制为“3a   30   31   30   32   30   30   30   30   30   30   30   31   46   43   0d   0a”。其中第一个为起始符,2-13为数据,14-15即“fc”为lrc校验码,有人能写一个取得校验位的函数吗?
下边有一段用vc写的程序

        将帧的内容,除去头代码,用十六进制表示,求和,模ff,然后取补码,以ascii码表示即可。
  例如:ascii帧
     3a   30   31   30   32   30   30   30   30   30   30   30   31   46   43   0d   0a
  将校验内容用十六进制表示为:
     00   01   00   02   00   00   00   00   00   00   00   01  
  将以上数值用十六进制求和,模ff:
     00+01+00+02+00+00+00+00+00+00+00+01=04=0000   0100
  取反:1111   1011
  加1:   1111   1100
  十六进制变换:f   c
  ascii码:46   43
  lrc就是这样算出来的

这里有一段用c写的程序,有谁能用delphi写吗?
        static   unsigned   char   lrc(auchmsg,usdatalen)
  
  unsigned   char   *auchmsg   ;   /*   要进行计算的消息   */
  
  unsigned   short   usdatalen   ;   /*   lrc   要处理的字节的数量*/
  
  {   unsigned   char   uchlrc   =   0   ;   /*   lrc   字节初始化   */
  
  while   (usdatalen--)   /*   传送消息   */  
  
  uchlrc   +=   *auchmsg++   ;   /*   累加*/  
  
  return   ((unsigned   char)(-((char_uchlrc)))   ;
  
  }
发表于:2008-01-10 16:59:121楼 得分:0
delphi(pascal) code
function getlrc(adata: array of byte; ilen: integer): byte; var iloop : integer; begin result := 0; for iloop := 0 to ilen - 1 do result := result + adata[iloop]; result := result xor $ff + 1; end; procedure tform1.formcreate(sender: tobject); var adata: array[0..5] of byte; ilrc : byte; begin adata[0] := $01; adata[1] := $02; adata[2] := $00; adata[3] := $00; adata[4] := $00; adata[5] := $01; ilrc := getlrc(adata, 6); showmessage(inttohex(ilrc,2)); end;
发表于:2008-01-10 17:03:022楼 得分:0
再多加个函数,可以直接获取类似楼主例子中的"010200000001fc"这个字符串:

delphi(pascal) code
function getlrc(adata: array of byte; ilen: integer): byte; var iloop : integer; begin result := 0; for iloop := 0 to ilen - 1 do result := result + adata[iloop]; result := result xor $ff + 1; end; function gethexstring(adata: array of byte; ilen: integer): string; var iloop: integer; begin result := ''; for iloop := 0 to ilen - 1 do result := result + inttohex(adata[iloop], 2); result := result + inttohex(getlrc(adata, ilen), 2); end; procedure tform1.formcreate(sender: tobject); var adata: array[0..5] of byte; begin adata[0] := $01; adata[1] := $02; adata[2] := $00; adata[3] := $00; adata[4] := $00; adata[5] := $01; showmessage(gethexstring(adata, 6)); end;
发表于:2008-01-12 11:10:123楼 得分:0
谢谢jadeluo
发表于:2008-01-12 11:14:394楼 得分:0
谢谢jadeluo


快速检索

最新资讯
热门点击