From: Aaron on
hi all,

I have written the following code to compute a CRC16-CCITT routine.
But a check shows that the calculated checksum is not correct. Can someone please help?
The calculated crc with the string '123456789' is 1DBA but the correct checksum should be 29B1.
I guess there is error with the string conversion b4 the checksum is carried out.

I


---------------------------------------------------------------
--------------------------------------------------------------
function crc = crc16 (str)
crc = uint16(hex2dec('FFFF'));%Initial remainder
a001 = uint16(hex2dec('1021')) %CRC16-CCITT polynomial

str = '123456789'

for i=1:length(str);
ByteOfMessage = str(i);
crc = bitxor(crc, real(ByteOfMessage));
for j=1:8
droppedbit=bitget(crc,1);
crc = bitshift(crc,-1);
if droppedbit;
crc = bitxor(crc,a001);
end
end
end
crc
disp (dec2hex(crc))

-----------------------------------------------------------------
-----------------------------------------------------------
From: Roger Stafford on
In article
<6008295.1132067245279.JavaMail.jakarta(a)nitrogen.mathforum.org>, Aaron
<aaronshchang(a)hotmail.com> wrote:

> hi all,
>
> I have written the following code to compute a CRC16-CCITT routine.
> But a check shows that the calculated checksum is not correct. Can
someone please help?
> The calculated crc with the string '123456789' is 1DBA but the correct
checksum should be 29B1.
> I guess there is error with the string conversion b4 the checksum is
carried out.
>
> ---------------------------------------------------------------
> --------------------------------------------------------------
> function crc = crc16 (str)
> crc = uint16(hex2dec('FFFF'));%Initial remainder
> a001 = uint16(hex2dec('1021')) %CRC16-CCITT polynomial
>
> str = '123456789'
>
> for i=1:length(str);
> ByteOfMessage = str(i);
> crc = bitxor(crc, real(ByteOfMessage));
> for j=1:8
> droppedbit=bitget(crc,1);
> crc = bitshift(crc,-1);
> if droppedbit;
> crc = bitxor(crc,a001);
> end
> end
> end
> crc
> disp (dec2hex(crc))
>
> -----------------------------------------------------------------
> -----------------------------------------------------------
Hello Aaron,

I am straining my memory about CRC checksums, but shouldn't your
"droppedbit" be taken from the left end? If bit numbering is from right
to left, that would make it droppedbit=bitget(crc,16), wouldn't it? (To
tell the truth I am not sure which way the bit positions are numbered.)

I don't see anything wrong with the "str = '123456789'" statement. Why
did you feel it necessary to use real() for the 'str' bytes? 'str' won't
have imaginary parts here. Perhaps you should convert the string
characters to 'uint8' or 'uint16' types before using them in 'bitxor'?

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
From: Aaron on
hi Roger,

I have tried "droppedbit=bitget (crc,16)", but this will give an answer of "0" as the checksum. I have also convertred the string to uint16.
I'm stucked now.
From: Roger Stafford on
In article
<6008295.1132067245279.JavaMail.jakarta(a)nitrogen.mathforum.org>, Aaron
<aaronshchang(a)hotmail.com> wrote:

> hi all,
>
> I have written the following code to compute a CRC16-CCITT routine.
> But a check shows that the calculated checksum is not correct. Can
someone please help?
> The calculated crc with the string '123456789' is 1DBA but the correct
checksum should be 29B1.
> I guess there is error with the string conversion b4 the checksum is
carried out.
>
> ---------------------------------------------------------------
> function crc = crc16 (str)
> crc = uint16(hex2dec('FFFF'));%Initial remainder
> a001 = uint16(hex2dec('1021')) %CRC16-CCITT polynomial
>
> str = '123456789'
>
> for i=1:length(str);
> ByteOfMessage = str(i);
> crc = bitxor(crc, real(ByteOfMessage));
> for j=1:8
> droppedbit=bitget(crc,1);
> crc = bitshift(crc,-1);
> if droppedbit;
> crc = bitxor(crc,a001);
> end
> end
> end
> crc
> disp (dec2hex(crc))
> -----------------------------------------------------------

In article
<33392666.1132220673050.JavaMail.jakarta(a)nitrogen.mathforum.org>, Aaron
<aaronshchang(a)hotmail.com> wrote:

> hi Roger,
>
> I have tried "droppedbit=bitget (crc,16)", but this will give an answer
of "0" as the checksum. I have also convertred the string to uint16.
> I'm stucked now.
------------------
Hello Aaron,

I would advise you to check out the websites:

http://www.ross.net/crc/crcpaper.html
http://www.ross.net/crc/download/crc_v3.txt
http://www.joegeluso.com/software/articles/ccitt.htm

Their contention is that hex 29B1 is the wrong CRC as CRC16-CCITT checksum
value for the string '123456789'. They say it ought to be hex E5CC. The
algorithm shown is different in a number of respects from the one you have
used. There is a large paper entitled, "A Painless Guide to CRC Error
Detection Algorithms" by Ross N. Williams, which you might find very
useful.

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford
From: Aaron on
Hi Roger,

I was actually using that "Painless" guide parallelly when writing this crc programme. I took to referring to the value hex 29B1 as the reference value to the string '123456789' just because a check using this string on a few online CRC-CCITT calculator delivered hex29B1 as the resulting value and not E5CC as delivered in the paper.. I will check again their differences especially with the difference between the results, 29B1 and E5CC as cited in the " http://www.joegeluso.com/software/articles/ccitt.htm" article.
Thanks for your help.