This returns
1010001 for your sample input. Is it correct? Is it efficient enough?
#!/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.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.