CRC-16/CCITT function

A simple function that converts text into the respective CRC code through bit operations. The code is in the description.

이 제출물을 팔로우합니다

function crc_value = crc_16(text)
% CRC_16 is a function with the objective of converting a text input into
% the respective CRC-CCITT code(outputed by crc_value). It uses as an
% initialization value 0xFFFF, and the standard CCITT polynomial:
% x^16 + x^12 + x^5 + 1
% It is worth noting that both the initial value and the polynomial can be
% easily changed in the code
% Initializing the most imoportant parameters
crc_value = 0xFFFF; % initial value
poly = 0x1021; % polynomial
text_length = strlength(text);
xor_flag = false;
for i=1:text_length
ch = uint16(text(i)); % initializing the character to go
ch = bitshift(ch,8); % through each iteration
% CRC bitwise operations
for n=1:8
if bitand(bitxor(crc_value,ch),0x8000)
xor_flag = true;
else
xor_flag = false;
end
crc_value = bitshift(crc_value,1);
if xor_flag
crc_value = bitxor(crc_value,poly);
end
ch = bitshift(ch,1);
end
end
% Converting the final value into an hexadecimal vector of characters
crc_value = dec2hex(crc_value); % comment if no conversion is necessary
%crc_value = string(crc_value); % uncomment to convert into MATLAB string
end
The code is based on the C source code in https://srecord.sourceforge.net/crc16-ccitt.html, the "bad_crc" part of the code. The algorithm is also known as CRC-16/CCITT-FALSE (I believe).

인용 양식

Francisco (2026). CRC-16/CCITT function (https://kr.mathworks.com/matlabcentral/fileexchange/127803-crc-16-ccitt-function), MATLAB Central File Exchange. 검색 날짜: .

태그

태그 추가

Add the first tag.

일반 정보

MATLAB 릴리스 호환 정보

  • 모든 릴리스와 호환

플랫폼 호환성

  • Windows
  • macOS
  • Linux
버전 퍼블리시됨 릴리스 정보 Action
1.0.1

-link was incorrect

1.0.0