in reply to Re^2: ECC computation
in thread ECC computation
#!/usr/bin/perl use warnings; use strict; use List::Util qw{ sum }; sub ecc { my ($hex) = @_; my $binary = pack 'H8', $hex; my @d = split //, unpack 'B32', $binary; my @p = map sum(map 0 + $d[$_], @$_) % 2, [ 0, 1, 3, 4, 6, 8, 10, 11, 13, 15, 17, 19, 21, 23, 25, 26, 28 +, 30 ], [ 0, 2, 3, 5, 6, 9, 10, 12, 13, 16, 17, 20, 21, 24, 25, 27, 28 +, 31 ], [ 1, 2, 3, 7, 8, 9, 10, 14, 15, 16, 17, 22, 23, 24, 25, 29, 30 +, 31 ], [ 4 .. 10, 18 .. 25 ], [ 11 .. 25 ], [ 26 .. 31 ], [ 0 .. 31 ]; $p[6] += sum(@p[ 0 .. 5 ]); $p[6] %= 2; return join "", @p } my $hex = '3E10F67A'; print ecc($hex);
Update: Added the 0 + which seems to speed up the sub by more than 60%. In a previous update, I used % 2 instead, which caused a speed-up of about 50%.
Update 2: fixed B64 to B32 (thanks LanX).
Update 3: fixed a bug: $p[6] must be % 2 separately from +=, otherwise it can get greater than 1.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: ECC computation
by LanX (Saint) on Jan 05, 2020 at 03:03 UTC | |
by choroba (Cardinal) on Jan 05, 2020 at 21:35 UTC | |
by LanX (Saint) on Jan 05, 2020 at 22:24 UTC | |
by choroba (Cardinal) on Jan 05, 2020 at 22:28 UTC | |
by LanX (Saint) on Jan 05, 2020 at 22:42 UTC | |
by savangadi (Initiate) on Jan 06, 2020 at 20:04 UTC | |
|