0

I have this work perl can support 4 hex numbers to swap in another 4 hex

perl -wMstrict -le '    
   my @bits = unpack "(A1)16", sprintf "%016b", hex shift;
   my $bitmap = "D5679123C4EF80AB";

   @bits = @bits[ map { hex } split //, $bitmap ];
   $"="";    

   print sprintf "%04X", oct "0b@bits";    
' "B455"

Result: CB15

please how can support more bytes like 128 bytes?

and how to use this perl to read the hex from a file.txt ?

thanks in advance.

  • still need help please – rabie chami Mar 04 '17 at 19:30
  • Can you provide a sample represetentative input file, the bitmapping, and the expected output? –  Mar 04 '17 at 20:22
  • You're asking us to write code, but that's off-topic. We might do it anyway, except you don't even specify what the code you want us to write should do! Do you simply want to change the instances of `16` to `1024` (the number of bits in 128 bytes)? – ikegami Mar 05 '17 at 06:10
  • As for passing the contents of a file as a parameter, `"$( cat file.txt )"` – ikegami Mar 05 '17 at 06:11

1 Answers1

0

You could try the following:

use feature qw(say);
use strict;
use warnings;

# Example with 64 bits
my $data = 'B455AB10A1230000'; # original data (64 bits)
my @bits = map { unpack '(A)*', sprintf '%08b', hex } unpack '(A2)*', $data;
my @bitmap = reverse 0..63;  # some 64 bits map, replace with your actual data

my $result = unpack "H*", pack 'C*', map { oct "0b$_" } unpack "(A8)*", join '', @bits[@bitmap];

say "Input : $data";
say "Result: $result";

Output:

Input : B455AB10A1230000
Result: 0000c48508d5aa2d
Håkon Hægland
  • 39,012
  • 21
  • 81
  • 174