谁能帮我编写一个使用16进制数进行相加并得出最后一位作为校验和的程序,还有如何把10进制数符给一个需要压缩BCD码中

解决方案 »

  1.   

    使用16进制数进行相加并得出最后一位作为校验和的程序function getsum(s: string): string;
    var
      i: integer;
      sum: integer;
    begin
      sum := 0;
      for i := 1 to round(length(s) / 2) do
      begin
        sum := sum + strtoint('$' + copy(s, i * 2 - 1, 2));
      end;
      result := inttohex((sum mod 256), 2);
    end;
      

  2.   

    另外,我如何把这个值转换成Char型的呢?调用的函数需要传入Char型的数组.
      

  3.   

    你的数据的总和有可能大于256,这样一个字节就无法保存了,所以一般都是取摸,
    保存最后一个字节。转换为char
    s:=getsum(str);
    c:=chr(strtoint('$'+s));