learningperl01 has asked for the wisdom of the Perl Monks concerning the following question:
What I am trying to do is process the output of the $hexchars variable but doing so 4 bytes at a time until no more data is left in the $hexchars variable. I am trying to reverse/swap two bytes from the 4 bytes that I am processing at a time. Example: the string "test" which I have in the input variable gets converted to: 74657374 in HEX. Now what I am trying to do is grab 4 bytes so in this case 7465 and swap/reverse them so they show up as so 6574. Then continue on to the next 4 characters and do the same until the variable is empty. Where I am having trouble is how to I process 4 characters at a time? Thanks for the help in advance.#!/usr/bin/perl use strict; use warnings; my $input_string = 'test'; my $hexchars = ''; foreach my $chars (split(//,$input_string)) { $hexchars .= sprintf "%x", ord($chars); } print "$input_string\n"; print "$hexchars\n"; my $length = length $hexchars; my $start = 0; my $end = $start + $length; print "length $length\n"; print "start_offset: $start\n"; print "end_offset: $end\n"; for ( $start = 0; $start < $length; $start ++ ) { print $start, "\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Process a set num of characters at a time
by moritz (Cardinal) on May 04, 2009 at 16:59 UTC | |
|
Re: Process a set num of characters at a time
by CountZero (Bishop) on May 04, 2009 at 17:17 UTC | |
|
Re: Process a set num of characters at a time
by toolic (Bishop) on May 04, 2009 at 17:06 UTC | |
|
Re: Process a set num of characters at a time
by jwkrahn (Abbot) on May 04, 2009 at 18:24 UTC | |
|
Re: Process a set num of characters at a time
by AnomalousMonk (Archbishop) on May 05, 2009 at 14:15 UTC | |
|
Re: Process a set num of characters at a time
by johngg (Canon) on May 05, 2009 at 11:18 UTC |