how to check whether my CRC32 is calculated correct

77 views (last 30 days)
Muhammad nasiri
Muhammad nasiri on 18 Nov 2017
Commented: Walter Roberson on 22 Nov 2021 at 9:53
I take input from user and calculate CRC32 for it, now I have the CRC32 for the input string, how to check if it is correct, in other words, how to decode back from CRC, any code will be appreciated
HERE is my code:
prompt = 'Please Enter your message \n';
Data = input(prompt,'s'); %Taking input from the user
crc = uint32(hex2dec('FFFFFFFF')); %Initalize CRC with all 32 bits (1's)
poly = uint32(hex2dec('EDB88320')); %Standard Polynomial of CRC32 in hex converted to decimals then to unsigned int32
data = uint8(Data); %Every char of input data is considered as one byte = 8 bits, and is converted to unisigned int8
% **** Computing CRC-32 value ****
for i = 1:length(data)
crc = bitxor(crc,uint32(data(i))); % type casting data to unisigned int32 inorder to be same as crc initial value type, then performing bitwise xor operations.
for j = 1:8 % Computing CRC on byte by byte basis (as required)
mask = bitcmp(bitand(crc,uint32(1))); %After performing bit wise AND operation, we take bit complement which is nothing but itself subtracted from the maximum integer of its data type
if mask == intmax('uint32'), mask = 0; %If value of mask even after taking complement is equal to maximum number of unit32 data type, we take its value as zero
else mask = mask+1;
end
crc = bitxor(bitshift(crc,-1),bitand(poly,mask));
end
end
crc_in_dec = bitcmp(crc);
crc = dec2bin(crc);
Looking forward to your answers
  2 Comments
Walter Roberson
Walter Roberson on 22 Nov 2021 at 9:53
Could you link to that 2014 post by Costas Vlachos ?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!