CRC8/Maxim function? lazarus / 21-General ---------------------------------------------------------------------- JCDes · 2021-07-02 19:59 Is there a crc8/maxim function available? I'm new to pascal and I just can't get it to work. I have searched and searched but nothing seems to work for me. Or maybe I'm just doing everything wrong. I have a hex editor window and need to generate the crc using the first 14 bytes.  I'm using the following code (opensource from the web): Code: [Select]CRC8 byte, normal var   CrcTable              : Array[0..255] of Cardinal; function GenerateTableCrc8(Poly:Cardinal):Cardinal; var   i                     : Cardinal; begin for i:=0 to 255 do CrcTable[i]:=Crc8(chr(i),Poly,0); end; function Crc8Byte(Buffer:String;Initial:Cardinal):Cardinal; var   i                     : Cardinal; begin Result:=Initial; for i:=1 to Length(Buffer) do begin   Result:=(Result shl 8) xor CrcTable[(ord(Buffer[i]) xor (Result)) and $ff];   end; Result:=Result and $ff; end; I am using $31 as the poly. since I'm such a a noob I get cofused in the order of things. I am using: mycrc := crc8(hexstring,poly,cardinal); Should I leave cardinal at 0? Or how does it work? I need to get the first 14 bytes of line $100 and generate the crc8/maxim from that. I'm lost... I know that the correcr crc for 00258974000022 is $5C since all the online calculators come up with the same value (sunshine, tomeko, crccalc etc.). But I can't seem to find the way. ---------------------------------------------------------------------- MarkMLl · 2021-07-02 21:03 When you say "can't get it to work", what /exactly/ do you mean? Can't compile? Result wrong? Messages... Where did you get that code? Is what you've supplied what you're trying to compile (please provide a complete program, not a fragment). Is the sequence to which you're applying a CRC a known test, and why are you confident you know what the result should be? When I was last looking at this sort of thing I found these useful: https://fossies.org/linux/peazip/crcmodel.pas https://fossies.org/linux/peazip/crcm_cat.pas I ended up implementing a function which could look at an arbitrary sequence and suggest which (known) CRC had been applied to it. I don't think it's in a state where it would be easy to drop your example into it, but overall it's the best collection I've come across. MarkMLl ---------------------------------------------------------------------- JCDes · 2021-07-03 02:30 1. My result is wrong. 2. I got that code from http://www.miscel.dk/MiscEl/CRCcalculations.html. 3. I am confident that I know the result because I got the original dump at hand. 4. I am using a script that runs on an a program that runs pascal scripts. My only obstacle is not being able to generate the crc8/maxim correctly for those 14 bytes. The software seems limited as for pascal commands for example Array[1...255] is not valid so I have to write the whole array which I did already earlier on. this is the whole function that I have so far it runs but does not calculate correctly. Once again I'm new to pascal. This is what I have done so far: Code: [Select]function crc8(Buffer:String;Poly,Initial:Cardinal):Cardinal; var   i,j                   : Integer; begin Result:=Initial; for i:=1 to Length(Buffer) do begin   Result:=Result xor ord(buffer[i]);   for j:=0 to 27 do begin     if (Result and $80)<>0 then Result:=(Result shl 1) xor $31     else Result:=Result shl 1;     end;   end; Result:=Result and $ff; end; ////////////////////////////////////////////////////////////////////// ////////// function GenerateTableCrc8(Poly:Cardinal):Cardinal; var   i                     : Cardinal;   CrcTable              : Array of Cardinal; begin CRCTABLE :=  [$00, $31, $62, $53, $C4, $F5, $A6, $97, $B9, $88, $DB, $EA, $7D, $4C, $1F, $2E , $43, $72, $21, $10, $87, $B6, $E5, $D4, $FA, $CB, $98, $A9, $3E, $0F, $5C, $6D , $86, $B7, $E4, $D5, $42, $73, $20, $11, $3F, $0E, $5D, $6C, $FB, $CA, $99, $A8 , $C5, $F4, $A7, $96, $01, $30, $63, $52, $7C, $4D, $1E, $2F, $B8, $89, $DA, $EB , $3D, $0C, $5F, $6E, $F9, $C8, $9B, $AA, $84, $B5, $E6, $D7, $40, $71, $22, $13 , $7E, $4F, $1C, $2D, $BA, $8B, $D8, $E9, $C7, $F6, $A5, $94, $03, $32, $61, $50 , $BB, $8A, $D9, $E8, $7F, $4E, $1D, $2C, $02, $33, $60, $51, $C6, $F7, $A4, $95 , $F8, $C9, $9A, $AB, $3C, $0D, $5E, $6F, $41, $70, $23, $12, $85, $B4, $E7, $D6 , $7A, $4B, $18, $29, $BE, $8F, $DC, $ED, $C3, $F2, $A1, $90, $07, $36, $65, $54 , $39, $08, $5B, $6A, $FD, $CC, $9F, $AE, $80, $B1, $E2, $D3, $44, $75, $26, $17 , $FC, $CD, $9E, $AF, $38, $09, $5A, $6B, $45, $74, $27, $16, $81, $B0, $E3, $D2 , $BF, $8E, $DD, $EC, $7B, $4A, $19, $28, $06, $37, $64, $55, $C2, $F3, $A0, $91 , $47, $76, $25, $14, $83, $B2, $E1, $D0, $FE, $CF, $9C, $AD, $3A, $0B, $58, $69 , $04, $35, $66, $57, $C0, $F1, $A2, $93, $BD, $8C, $DF, $EE, $79, $48, $1B, $2A , $C1, $F0, $A3, $92, $05, $34, $67, $56, $78, $49, $1A, $2B, $BC, $8D, $DE, $EF , $82, $B3, $E0, $D1, $46, $77, $24, $15, $3B, $0A, $59, $68, $FF, $CE, $9D, $AC]; for i := $0 to $FF do CrcTable[i]:=Crc8(chr(i),$31,0); end; ////////////////////////////////////////////////////////////////////// ////////// function Crc8Byte(Buffer:String;Initial:Cardinal):Cardinal; var   i                     : Cardinal;   crctable: array of cardinal; begin CRCTABLE :=  [$00, $31, $62, $53, $C4, $F5, $A6, $97, $B9, $88, $DB, $EA, $7D, $4C, $1F, $2E , $43, $72, $21, $10, $87, $B6, $E5, $D4, $FA, $CB, $98, $A9, $3E, $0F, $5C, $6D , $86, $B7, $E4, $D5, $42, $73, $20, $11, $3F, $0E, $5D, $6C, $FB, $CA, $99, $A8 , $C5, $F4, $A7, $96, $01, $30, $63, $52, $7C, $4D, $1E, $2F, $B8, $89, $DA, $EB , $3D, $0C, $5F, $6E, $F9, $C8, $9B, $AA, $84, $B5, $E6, $D7, $40, $71, $22, $13 , $7E, $4F, $1C, $2D, $BA, $8B, $D8, $E9, $C7, $F6, $A5, $94, $03, $32, $61, $50 , $BB, $8A, $D9, $E8, $7F, $4E, $1D, $2C, $02, $33, $60, $51, $C6, $F7, $A4, $95 , $F8, $C9, $9A, $AB, $3C, $0D, $5E, $6F, $41, $70, $23, $12, $85, $B4, $E7, $D6 , $7A, $4B, $18, $29, $BE, $8F, $DC, $ED, $C3, $F2, $A1, $90, $07, $36, $65, $54 , $39, $08, $5B, $6A, $FD, $CC, $9F, $AE, $80, $B1, $E2, $D3, $44, $75, $26, $17 , $FC, $CD, $9E, $AF, $38, $09, $5A, $6B, $45, $74, $27, $16, $81, $B0, $E3, $D2 , $BF, $8E, $DD, $EC, $7B, $4A, $19, $28, $06, $37, $64, $55, $C2, $F3, $A0, $91 , $47, $76, $25, $14, $83, $B2, $E1, $D0, $FE, $CF, $9C, $AD, $3A, $0B, $58, $69 , $04, $35, $66, $57, $C0, $F1, $A2, $93, $BD, $8C, $DF, $EE, $79, $48, $1B, $2A , $C1, $F0, $A3, $92, $05, $34, $67, $56, $78, $49, $1A, $2B, $BC, $8D, $DE, $EF , $82, $B3, $E0, $D1, $46, $77, $24, $15, $3B, $0A, $59, $68, $FF, $CE, $9D, $AC]; Result := Initial; for i := 1 to Length(Buffer) do begin   Result := (Result shl 8) xor CrcTable[(ord(Buffer[i]) xor (Result)) and $ff];   end; Result := Result and $ff; end; Then I'm using it like this: Code: [Select]Var1 :=     IntToHex(GetByteHexEdit($00),2) +  IntToHex(GetByteHexEdit($01),2) +  IntToHex(GetByteHexEdit($02),2) +  IntToHex(GetByteHexEdit($03),2) +  IntToHex(GetByteHexEdit($04),2) +  IntToHex(GetByteHexEdit($05),2) +  IntToHex(GetByteHexEdit($06),2) +  IntToHex(GetByteHexEdit($07),2) +  IntToHex(GetByteHexEdit($08),2) +  IntToHex(GetByteHexEdit($0A),2) +  IntToHex(GetByteHexEdit($0B),2) +  IntToHex(GetByteHexEdit($0C),2) +  IntToHex(GetByteHexEdit($0D),2); Var1 := Crc8byte(var1,0); SetByteHexEdit ($11F,Var1); ---------------------------------------------------------------------- MarkMLl · 2021-07-03 06:24 What EXACTLY goes wrong? Please give us a COMPLETE program and some test data to work with. Look, I want to help but I've got some important bookkeeping work to do. Pretty much everybody else has other demands on their time. MarkMLl ---------------------------------------------------------------------- avk · 2021-07-03 14:42 Googling shows that CRC8/Maxim uses the following parameters: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   CRC polynomial: $31  Initial value:  $00  Final xormask:  $00  Reflect input:  True  Reflect output: True  Accordingly, the function should be like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Crc8Maxim(aData: PByte; aSize: SizeInt): Cardinal;  function ReflectLoByte(aValue: Cardinal): Cardinal; inline;  begin    aValue :=        (aValue and $f0) shr 4 or (aValue and $0f) shl 4;    aValue :=      (aValue and $cc) shr 2 or (aValue and $33) shl 2;    ReflectLoByte := (aValue and $aa) shr 1 or (aValue and $55) shl 1;  end;var  I, J: SizeInt;begin  Result := 0;  for I := 0 to Pred(aSize) do    begin      Result := Result xor ReflectLoByte(Cardinal(aData[I]));      for J := 1 to 8 do        Result := Result shl 1 xor $31 and SarShortInt(Result, 7);    end;  Result := ReflectLoByte(Result) and $ff;end; Or a faster version using a lookup table: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const  CrcTable: array[Byte] of Cardinal = (    $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,    $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,    $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,    $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,    $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,    $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,    $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,    $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,    $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,    $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,    $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,    $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,    $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,    $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,    $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,    $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35); function Crc8Maxim(aData: PByte; aSize: SizeInt): Cardinal;var  I: SizeInt;begin  Result := 0;  for I := 0 to Pred(aSize) do    Result := CrcTable[Result xor aData[I]];end; ---------------------------------------------------------------------- JCDes · 2021-07-03 16:42 I really appreciate your guidance. This software that I'm using allows the user to write our own scripts (which is what I'm trying to do) I have written many others but never a crc function or procedure. Maybe that is the source of my problem? TMS Scripts seem to run on limited libraries and types. For Example nested functions return syntax error, const CrcTable: array[Byte] of Cardinal = also returns syntax error. ---------------------------------------------------------------------- JCDes · 2021-07-03 16:54 [0..255] errors out also as syntax error... I found some more info about the scripter: VarArrayCreate VarArrayHighBound VarArrayLowBound function SetOf(array): integer; //Returns the current scripter component. MyFontStyle := SetOf([fsBold, fsItalic]);//Returns a set from the array passed. For example: Pascal syntax supports: begin .. end constructor procedure and function declarations if .. then .. else constructor for .. to .. do .. step constructor while .. do constructor repeat .. until constructor try .. except and try .. finally blocks case statements array constructors (x:=[ 1, 2, 3 ] ^ , * , / , and , + , - , or , <> , >=, <= , = , > , < , div , mod , xor , shl , shr operators access to object properties and methods ( ObjectName.SubObject.Property ) Arrays Script support array constructors and support to variant arrays. To construct an array, use "[" and "]" chars. You can construct multi-index array nesting array constructors. You can then access arrays using indexes. If array is multi-index, separate indexes using ",". If variable is a variant array, script automatically support indexing in that variable. A variable is a variant array is it was assigned using an array constructor, if it is a direct reference to a Delphi variable which is a variant array (see Delphi integration later) or if it was created using VarArrayCreate procedure. Arrays in script are 0-based index. Some examples: NewArray := [ 2,4,6,8 ]; Num:=NewArray[1]; //Num receives "4" MultiArray := [ ['green','red','blue'] , ['apple','orange','lemon'] ]; Str:=MultiArray[0,2]; //Str receives 'blue' MultiArray[1,1]:='new orange'; Indexes Strings, arrays and array properties can be indexed using "[" and "]" chars. For example, if Str is a string variable, the expression Str[3] returns the third character in the string denoted by Str, while Str[I + 1] returns the character immediately after the one indexed by I. More examples: MyChar:=MyStr[2]; MyStr[1]:='A'; MyArray[1,2]:=1530; Lines.Strings[2]:='Some text'; ---------------------------------------------------------------------- JCDes · 2021-07-03 16:57 Quote from: MarkMLl on July 03, 2021, 08:24:26 amWhat EXACTLY goes wrong? Please give us a COMPLETE program and some test data to work with. Look, I want to help but I've got some important bookkeeping work to do. Pretty much everybody else has other demands on their time. MarkMLl Basically what goes wrong is that due to my lack of knowledge I am getting a wrong value for the crc calculation result. No errors just a bad calculation and maybe a tms scripter that uses limited pascal syntax. ---------------------------------------------------------------------- avk · 2021-07-03 17:29 Never used TMS Scripter. Curious, but will such a code be workable? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Crc8Maxim(Buffer: string):Cardinal;var  CrcTable: array of Cardinal;  I: LongInt;begin  CrcTable := [    $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,    $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,    $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,    $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,    $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,    $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,    $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,    $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,    $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,    $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,    $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,    $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,    $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,    $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,    $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,    $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35];   Result := 0;  for I := 1 to Length(Buffer) do    Result := CrcTable[Result xor Ord(Buffer[I])];end; ---------------------------------------------------------------------- MarkMLl · 2021-07-03 18:16 Quote from: JCDes on July 03, 2021, 06:57:34 pmBasically what goes wrong is that due to my lack of knowledge I am getting a wrong value for the crc calculation result. No errors just a bad calculation and maybe a tms scripter that uses limited pascal syntax. So you're not using FPC (Free or Florian's Pascal compiler... that's obviously going to be a problem since generally speaking people here are going to be using FPC's extended syntax to define array contents etc... you'll probably need to populate arrays using code rather than as a constant. Please could we have your test data so that we can see what your test cases are vs your expected results. MarkMLl ---------------------------------------------------------------------- avk · 2021-07-04 03:11 BTW, CRC8/Maxim has a check value of $a1, that is, the Crc8Maxim('123456789') function should return $a1. ---------------------------------------------------------------------- JCDes · 2021-07-05 15:59 Quote from: avk on July 03, 2021, 07:29:19 pmNever used TMS Scripter. Curious, but will such a code be workable? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Crc8Maxim(Buffer: string):Cardinal;var  CrcTable: array of Cardinal;  I: LongInt;begin  CrcTable := [    $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,    $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,    $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,    $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,    $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,    $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,    $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,    $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,    $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,    $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,    $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,    $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,    $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,    $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,    $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,    $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35];   Result := 0;  for I := 1 to Length(Buffer) do    Result := CrcTable[Result xor Ord(Buffer[I])];end;  No errors at all... But The result is not the expected one. 00 00 00 05 00 00 00 00 00 00 00 00 00 00  is giving me "5A" When I check against the crc8/maxim online calculators the result is "AA" just like the original dump I have. https://tomeko.net/online_tools/crc8.php?lang=en https://crccalc.com/ http://www.sunshine2k.de/coding/javascript/crc/crc_js.html ---------------------------------------------------------------------- avk · 2021-07-05 17:02 I assume your string(00 00 00 05 00 00 00 00 00 00 00 00 00 00) is a sequence of bytes in hexadecimal notation. It can be represented as an array of bytes   b = [0,0,0,5,0,0,0,0,0,0,0,0,0,0], or the string   s = #0#0#0#5#0#0#0#0#0#0#0#0#0#0, for both array and string, Crc8Maxim() (any of the above) returns $aa. ---------------------------------------------------------------------- JCDes · 2021-07-05 23:40 Yes it is a a sequence of bytes in hex notation that I read from an eeprom dump. It is never the same sequence. ---------------------------------------------------------------------- JCDes · 2021-07-06 01:42 It's 8 bit... Every eeprom dump that I verify against the three online crc8/maxim generators give me the same results on the specific lines. I even modify the values and use the online generators and get no errors on the hardware display when I write the eeprom and solder back to the board with modified values. Maybe I'm reading the bytes wrong? ---------------------------------------------------------------------- avk · 2021-07-06 09:20 In my previous post, I tried to draw your attention to the fact that all the functions mentioned above expect a sequence of bytes as input, and not their hexadecimal codes. And by the way, have you tried passing the test string '123456789' to your instance of the CRC function? What result does it return? ---------------------------------------------------------------------- MarkMLl · 2021-07-06 09:46 There's also a possibility that, since it's apparently not FPC, the compiler is making different size assumptions etc. e.g. that cardinals are 8-bit unsigned. MarkMLl ---------------------------------------------------------------------- JCDes · 2021-07-06 16:17 It is calculating as if the data was a string not bytes. "0000000500000000000000000000" calculates to "03" which is perfect for string input. How can I get it to interpret the input as bytes $00 $00 $00 $05 $00 $00 $00 $00 $00 $00 $00 $00 $00 $00 to get "AA" result?? ---------------------------------------------------------------------- lucamar · 2021-07-06 17:05 A quick way would be to add a first step to convert the input string to another one with each pair of digits from the original converted to the corresponding character, e.g. with something like this (untested) function: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function Recode(const Origin: String): String;var  sPos: Integer = 1;begin  Result := '';   while sPos < Length(Origin) do begin    Result := Result + Chr(StrToInt('$'+ Original.SubString(sPos,2)));    Inc(sPos, 2)  end;end; ---------------------------------------------------------------------- JCDes · 2021-07-06 18:23 Error: Object or record required to access method "Substring". on this line: Result := Result + Chr(StrToInt('$'+ Origin.SubString(sPos,2))); ---------------------------------------------------------------------- lucamar · 2021-07-06 19:24 Quote from: JCDes on July 06, 2021, 08:23:49 pmError: Object or record required to access method "Substring". on this line: Result := Result + Chr(StrToInt('$'+ Origin.SubString(sPos,2))); Do you have SysUtils in your uses clause? SubString() is a method of TStringHelper, the string type helper defined there. If for some reason you don't want to use it you can use the standard Copy() function; it's basically the same: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Result := Result + Chr(StrToInt('$'+ Copy(Origin, sPos, 2))); ---------------------------------------------------------------------- JCDes · 2021-07-06 21:56 Expected 1 parameter error on line: Inc(sPos, 2) ---------------------------------------------------------------------- JCDes · 2021-07-06 22:15 This is the code that is calculating the data as a string correctly: Code: [Select]function Crc8Maxim(Buffer: string):Cardinal; var   CrcTable: array of Cardinal;   I: LongInt; begin   CrcTable := [     $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,     $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,     $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,     $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,     $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,     $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,     $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,     $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,     $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,     $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,     $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,     $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,     $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,     $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,     $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,     $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35];   Result := 0;   for I := 1 to Length(Buffer) do     Result := CrcTable[Result xor Ord(Buffer[I])]; end; This is how I'm reading the data from the hex editor: Code: [Select]CS := IntToHex(GetByteHexEdit( $00),2) + IntToHex(GetByteHexEdit( $01),2) + IntToHex(GetByteHexEdit( $02),2) + IntToHex(GetByteHexEdit( $03),2) + IntToHex(GetByteHexEdit( $04),2) + IntToHex(GetByteHexEdit( $05),2) + IntToHex(GetByteHexEdit( $06),2) + IntToHex(GetByteHexEdit( $07),2) + IntToHex(GetByteHexEdit( $08),2) + IntToHex(GetByteHexEdit( $09),2) + IntToHex(GetByteHexEdit( $0A),2) + IntToHex(GetByteHexEdit( $0B),2) + IntToHex(GetByteHexEdit( $0C),2) + IntToHex(GetByteHexEdit( $0D),2); Ir reads the data I need and gets processed but it gets processed as a string not as bytes. If I go to http://www.sunshine2k.de/coding/javascript/crc/crc_js.html and set to crc-maxim in the predifined section and select "string" I get the same result but the result I need is the one for the "bytes" setting. "0000000500000000000000000000" I get "03" but I should be getting "AA" ---------------------------------------------------------------------- lucamar · 2021-07-06 22:36 Quote from: JCDes on July 06, 2021, 11:56:31 pmExpected 1 parameter error on line: Inc(sPos, 2) A wild guess: Some other unit in your uses is hijacking the one declared in System. Replace with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i System.Inc(sPos, 2) ---------------------------------------------------------------------- JCDes · 2021-07-06 22:55 Error, it took "system" as an undeclared variable. This tms scripter is killing me. I really appreciate you guys  helping me even if I'm not using fpc. I'm using tms scripter and it seems like the pascal syntax is limited. This is all the documentation that comes with it. https://doc.tmssoftware.com/biz/scripter/scripter-user-guide.pdf ---------------------------------------------------------------------- lucamar · 2021-07-07 01:45 Quote from: JCDes on July 07, 2021, 12:55:15 amThis tms scripter is killing me. Oops! Is it an script? No wonder it barked at the type helper, then! Sorry, didn't read enough of the thread ... If all else is working then try replacing the single Inc(sPos, 2) with either: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Inc(sPos);Inc(sPos);or with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i sPos := sPos + 2;Either of them should work, according to the documentation and the error message. ETA: I don't want to sound snarky but ... rather than copy-pasting you should try to understand what our code is trying to do so you can replace it with the appropiate one for TMS Script. In this particular case it should have been clear that it's increasing sPos by 2, so the solution(s) should have been obvious to you ... Just saying ---------------------------------------------------------------------- avk · 2021-07-07 04:33 Hmm, what if something like that? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  CS := Char(GetByteHexEdit( 0)) + Char(GetByteHexEdit( 1)) + Char(GetByteHexEdit( 2)) +       Char(GetByteHexEdit( 3)) + Char(GetByteHexEdit( 4)) + Char(GetByteHexEdit( 5)) +       Char(GetByteHexEdit( 6)) + Char(GetByteHexEdit( 7)) + Char(GetByteHexEdit( 8)) +       Char(GetByteHexEdit( 9)) + Char(GetByteHexEdit(10)) + Char(GetByteHexEdit(11)) +       Char(GetByteHexEdit(12)) + Char(GetByteHexEdit(13)); That is, you can try to get a byte sequence at once and do without further conversions. ---------------------------------------------------------------------- Thaddy · 2021-07-07 08:03 Ahum, https://www.freepascal.org/docs-html/rtl/sysutils/tbytes.html Also note the "see also" ---------------------------------------------------------------------- JCDes · 2021-07-07 19:08 I really appreciate your help. The problem is that this TMS Scripter that I'm using errors out on:  Const CrcTable:Array[0..255] of Cardinal = <------- Syntax error inc(I,2) <----- This one says expected 1 parameter only it allows var  CRCTable:array; begin crctable := [the table here]; ---------------------------------------------------------------------- MarkMLl · 2021-07-08 08:09 Quote from: JCDes on July 07, 2021, 09:08:49 pmI really appreciate your help. The problem is that this TMS Scripter that I'm using errors out on:  Const CrcTable:Array[0..255] of Cardinal = <------- Syntax error inc(I,2) <----- This one says expected 1 parameter only it allows var  CRCTable:array; begin crctable := [the table here]; Isn't that table generated by a function you gave in your first posting? Use that to generate the CRC table as a variable (which will be standard 1970s-style Pascal) rather than using language extensions to predefine it as a constant. MarkMLl ---------------------------------------------------------------------- avk · 2021-07-08 12:43 Somehow I suddenly remembered that Lazarus has a set of Pascal Script components, probably having a common origin with TMS Scripter. The results of running this test script Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program CrcTest; function ReflectLoByte(aValue: Cardinal): Cardinal;begin  aValue := (aValue and $f0) shr 4 or (aValue and $0f) shl 4;  aValue := (aValue and $cc) shr 2 or (aValue and $33) shl 2;  Result := (aValue and $aa) shr 1 or (aValue and $55) shl 1;end; function Crc8Maxim(const aData: string): Cardinal;var  I, J: LongInt;begin  Result := 0;  for I := 1 to Length(aData) do    begin      Result := Result xor ReflectLoByte(Ord(aData[I]));      for J := 1 to 8 do        if Result and $80 <> 0 then          Result := Result shl 1 xor $31        else          Result := Result shl 1;    end;  Result := ReflectLoByte(Result) and $ff;end; function Crc8Maxim_2(const aBuffer: string): Cardinal;var  CrcTable: array of Cardinal;  I: LongInt;begin  CrcTable := [    $00, $5e, $bc, $e2, $61, $3f, $dd, $83, $c2, $9c, $7e, $20, $a3, $fd, $1f, $41,    $9d, $c3, $21, $7f, $fc, $a2, $40, $1e, $5f, $01, $e3, $bd, $3e, $60, $82, $dc,    $23, $7d, $9f, $c1, $42, $1c, $fe, $a0, $e1, $bf, $5d, $03, $80, $de, $3c, $62,    $be, $e0, $02, $5c, $df, $81, $63, $3d, $7c, $22, $c0, $9e, $1d, $43, $a1, $ff,    $46, $18, $fa, $a4, $27, $79, $9b, $c5, $84, $da, $38, $66, $e5, $bb, $59, $07,    $db, $85, $67, $39, $ba, $e4, $06, $58, $19, $47, $a5, $fb, $78, $26, $c4, $9a,    $65, $3b, $d9, $87, $04, $5a, $b8, $e6, $a7, $f9, $1b, $45, $c6, $98, $7a, $24,    $f8, $a6, $44, $1a, $99, $c7, $25, $7b, $3a, $64, $86, $d8, $5b, $05, $e7, $b9,    $8c, $d2, $30, $6e, $ed, $b3, $51, $0f, $4e, $10, $f2, $ac, $2f, $71, $93, $cd,    $11, $4f, $ad, $f3, $70, $2e, $cc, $92, $d3, $8d, $6f, $31, $b2, $ec, $0e, $50,    $af, $f1, $13, $4d, $ce, $90, $72, $2c, $6d, $33, $d1, $8f, $0c, $52, $b0, $ee,    $32, $6c, $8e, $d0, $53, $0d, $ef, $b1, $f0, $ae, $4c, $12, $91, $cf, $2d, $73,    $ca, $94, $76, $28, $ab, $f5, $17, $49, $08, $56, $b4, $ea, $69, $37, $d5, $8b,    $57, $09, $eb, $b5, $36, $68, $8a, $d4, $95, $cb, $29, $77, $f4, $aa, $48, $16,    $e9, $b7, $55, $0b, $88, $d6, $34, $6a, $2b, $75, $97, $c9, $4a, $14, $f6, $a8,    $74, $2a, $c8, $96, $15, $4b, $a9, $f7, $b6, $e8, $0a, $54, $d7, $89, $6b, $35];   Result := 0;  for I := 1 to Length(aBuffer) do    Result := CrcTable[Result xor Ord(aBuffer[I])];end; function HexDecode(const aOrigin: string): string;var  I, CurPos: LongInt;begin  CurPos := 1;  SetLength(Result, Length(aOrigin) div 2 + Length(aOrigin) and 1);  for I := 1 to Length(Result) do    begin      Result[I] := Chr(StrToInt('$' + Copy(aOrigin, CurPos, 2)));      CurPos := CurPos + 2;    end;end; function GetByteHexEdit(aIndex: LongInt): Byte;begin  if aIndex = 3 then    Result := 5  else    Result := 0;end; function GetByteSequence: string;var  I: LongInt;begin  SetLength(Result, 14);  for I := 1 to Length(Result) do    Result[I] := Chr(GetByteHexEdit(I - 1));end; var  s: string;  c: Cardinal;begin  WriteStr('Crc8Maxim results:');  WriteStr('self test:');  s := '123456789';  c := Crc8Maxim(s);  WriteCardAsHex(c);  WriteStr('hexadecimal string:');  s := '0000000500000000000000000000';  c := Crc8Maxim(HexDecode(s));  WriteCardAsHex(c);  WriteStr('byte sequence:');  s := GetByteSequence;  c := Crc8Maxim(s);  WriteCardAsHex(c);  WriteStr('');   WriteStr('Crc8Maxim_2 results:');  WriteStr('self test:');  s := '123456789';  c := Crc8Maxim_2(s);  WriteCardAsHex(c);  WriteStr('hexadecimal string:');  s := '0000000500000000000000000000';  c := Crc8Maxim_2(HexDecode(s));  WriteCardAsHex(c);  WriteStr('byte sequence:');  s := GetByteSequence;  c := Crc8Maxim_2(s);  WriteCardAsHex(c);end.  in TPSScript: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Crc8Maxim results:self test:A1hexadecimal string:AAbyte sequence:AA Crc8Maxim_2 results:self test:A1hexadecimal string:AAbyte sequence:AA WriteStr and WriteCardAsHex are helper user-defined functions for outputting results to TMemo. ---------------------------------------------------------------------- JCDes · 2021-07-08 17:11 I get error on HexDecode: Unknown method or routine "SetLength".  Length works but setlength does not seem to be part of the syntax supported. "function Crc8Maxim_2(const aBuffer: string): Cardinal;" errors out unless I remove "const" so it looks like: " function Crc8Maxim_2(aBuffer: string): Cardinal;" . ---------------------------------------------------------------------- JCDes · 2021-07-08 17:35 It's working! ---------------------------------------------------------------------- avk · 2021-07-08 17:39 Hmm, TMS Scripter lags behind Pascal Script in functionality? Anyway, I'm glad that you finally succeed. ---------------------------------------------------------------------- JCDes · 2021-07-08 18:10 Actually it was all of you and your great effort to help me. All I did was play with the syntax that you already had given me. Once again thank you all and specially you avk. ---------------------------------------------------------------------- avk · 2021-07-09 16:07 You are welcome.