Re: hexadecimal calculating: turning on the high bit



HanslH wrote:
Communicating (serial) from delphi with a device I have to conform to
some format.
One of the rules is that a two byte trailer should consist of a
checksum byte and an end byte (always 0x03)

0x03 is also referred to as "ETX" end of transmission. It tells
the other computer (or device) that the packet has ended,
go ahead and process what you've got.

Are you using a STX (0x02) at the head of your transmission to
tell the device that you're starting a set?

<snip>

I find that all examples in the reference *but one* will be calculated
successfully by the following algorithm
//======================================
function VetmessageChecksum(vetmessage : string) : string;
var
i:integer;
cHex : string;
aDec : arrayofinteger;
iChecksum : integer;
begin
cHex := '';

//make sure it ends with a space because of easier looping
if lastchar(vetmessage) <> ' ' then vetmessage := vetmessage + ' ';
//turning a '23 4F 45 22' type of string into an array of integer
for i := 1 to length(vetmessage) do begin
if (vetmessage[i]) = ' ' then begin
aadd(aDec,strtoint('$' + trim(cHex)));

/me giggles at the Clipper reference...

cHex := '';
end else
cHex := cHex + vetmessage[i];
end;

//summing the value iChecksum := 0;
for i := 0 to length(aDec) -1 do iChecksum := iChecksum + aDec[i];

/next ensures the *unsigned* char thing?
//I'm no expert but it's a trial and error assumption iChecksum := iChecksum mod 256;
result := inttohex(iChecksum,2);

end;
//========================================


Requiring a space to delimit the hex numbers is questionable, it would
be better to loop through taking two character bits until you don't have
any more two character bits left.

Break this up and write a hex converter, send each two character set
to it and add them up. There are far better methods of checksumming
than just addition, that can screw up quite easily, be wrong, but
still come up with the proper answer. Do some googling for "CRC".

if iChecksum and 256 = 256 then iChecksum - 256 should remove the
high bit, but I'm not sure that is what your teacher wanted you to do
by the sounds of it.

.



Relevant Pages

  • Re: hexadecimal calculating: turning on the high bit
    ... function VetmessageChecksum(vetmessage: string): string; ... cHex: string; ... iChecksum: integer; ... I'd also calculate the checksum along the way, instead of doing it as a separate loop. ...
    (alt.comp.lang.borland-delphi)
  • Re: Xor encryption
    ... If message is 'bas' the checksum is '04'. ... Or do I need a key to Xor with? ... Function xorit(ByVal s As String) As String ... Dim Character As Char ...
    (sci.crypt)
  • hexadecimal calculating: turning on the high bit
    ... I have hardly any experience with hexadecimal calculating and 'and' ... cHex: string; ... iChecksum: integer; ... I find that the only example that doesnt compute right the checksum ...
    (alt.comp.lang.borland-delphi)
  • =?ISO-8859-1?Q?Serialport_und_Encoding=3B_Problem_beim_Konvertieren_A?= =?ISO-8859-1
    ... Meine Umsetzung sieht so aus: ... sReadByte = sLine.Substring ... iChecksum += Asc ... Wenn aber sLine z.B. den Wert 'test' hat, wird die Checksum vom ...
    (microsoft.public.de.german.entwickler.dotnet.vb)