Hi Monks,
I want to determine the CRC of a string as follows:
I split the input string in 4 byte blocks. Then I take the value of the 4 bytes (big endian) and add the value to the next 4 bytes of the string, ...
If the last part of the string consists of less than 4 bytes then I pad the not available bytes with "00".
By building the sum it could happen that the value gets bigger than 4 bytes. So I take a modulo 0x100000000
This modulo operation leads to a warning:
Integer overflow in hexadecimal number at generateLoadFile.pl line 27. Hexadecimal number > 0xffffffff non-portable at generateLoadFile.pl li +ne 27.
Here my bad code for doing this. It is working but I don't like it:
#!/usr/bin/perl use strict; use warnings; my $buffer = ""; my $i = 0; my $crc = 0; my $val = ""; my $k = 0; # solution: 990644297 $buffer = "/etc/test2.sh"; # Integer overflow in hexadecimal number at generateLoadFile.pl line 2 +8. # Hexadecimal number > 0xffffffff non-portable at generateLoadFile.pl +line 45. # Integer overflow in hexadecimal number at generateLoadFile.pl line 2 +8. # Hexadecimal number > 0xffffffff non-portable at generateLoadFile.pl +line 45. for( $i = 0; $i < int(length($buffer) / 4); $i = $i + 1 ) { $val = sprintf("%02X", ord(substr($buffer, ($i*4) + 0, 1))) . sprintf("%02X", ord(substr($buffer, ($i*4) + 1, 1))) . sprintf("%02X", ord(substr($buffer, ($i*4) + 2, 1))) . sprintf("%02X", ord(substr($buffer, ($i*4) + 3, 1))); $crc = ($crc + hex($val)) % 0x100000000; } $val = ""; $k = 0; for( 1 .. int(length($buffer) % 4) ) { $val .= sprintf("%02X", ord(substr($buffer, ($i*4) + $k, 1))); $k++; } # pad other bytes of 32bit with "00" for( 1 .. (4 - int(length($buffer) % 4)) ) { $val .= "00"; } $crc = ($crc + hex($val)) % 0x100000000; print $crc . "\n";
The CRC of the string "/etc/test2.sh" is 990644297.
Now my questions:
Thank you very much
DirkIn reply to CRC of a string by Dirk80
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |