how to check whether my CRC32 is calculated correct
34 次查看(过去 30 天)
显示 更早的评论
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 个评论
Kamal
2021-11-22
Interesting how you add 4 lines around the top and bottom and now it becomes your code. This is taken from Costas Vlachos' 2014 post. Why not just use his function?
回答(1 个)
Walter Roberson
2017-11-18
Call https://www.mathworks.com/matlabcentral/fileexchange/49518-crc-32-computation-algorithm and see if you get the same result.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!