MATLAB: Converting vector to binary and summing it

dec2binsum

Greetings,
I have a large vector 2048 x 1 of numbers I need to convert into binary and add them all togather (disregarding overflow) to create a checksum. Can I convert all the numbers to binary with a
dec2bin( decimal number) followed by a sum(vector)
Commands? This data is written to flash memory and we need to impliment a checksum
Thanks

Best Answer

What kind of checksum are you talking of?
If the numbers have a range of 0-255 a simple CRC32 could be helpful:
% Create CRC lookup table:
CRC_Table = zeros(1, 256);
POLYNOMIAL = 79764919; % 0x04c11db7L
shift24 = 16777216;
for i = 0:255
% crc = bitshift(i, 24);
crc = i * shift24; % Faster
for j = 0:7
if crc >= 2147483648 % if bitget(crc, 32)
%crc = bitand(bitxor(crc * 2, POLYNOMIAL), 4294967295);
crc = rem(bitxor(crc * 2, POLYNOMIAL), 4294967296);
else
crc = crc * 2;
end
end % for j
% CRC_Table(i + 1) = bitand(crc, 4294967295);
CRC_Table(i + 1) = rem(crc, 4294967296);
end % for i
S = 0;
c1 = 16777216; % 2^24
c3 = 256;
c5 = 4294967296; % 2^32: REM instead of BITAND
for n = 1:length(Data)
m = rem(bitxor(fix(S / c1), Data(n)), c3);
S = bitxor(rem(S * c3, c5), CRC_Table(m + 1));
end
S = sprintf('%.8X', S);
For another range, you can cast the values:
Data = typecast(Data, 'uint8');
Further hash methods can be found in: FEX: DataHash. E.g.:
Engine = java.security.MessageDigest.getInstance('SHA-256');
Engine.update(tyepcast(Data, 'uint8'));
Hash = typecast(Engine.digest, 'uint8');
HashHex = sprintf('%.2x', Hash);