Re: hexadecimal calculating: turning on the high bit
- From: Erik <erikpub@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 22 Nov 2006 16:50:20 -0800
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.
.
- Follow-Ups:
- References:
- hexadecimal calculating: turning on the high bit
- From: HanslH
- hexadecimal calculating: turning on the high bit
- Prev by Date: hexadecimal calculating: turning on the high bit
- Next by Date: Re: hexadecimal calculating: turning on the high bit
- Previous by thread: hexadecimal calculating: turning on the high bit
- Next by thread: Re: hexadecimal calculating: turning on the high bit
- Index(es):
Relevant Pages
|