in reply to Re: Binary conditionals
in thread Binary conditionals

Ok, I can't do a simple s/// because I actually have to rotate them. It's something with the conditional or the assignment. I tried updating the code in this manner
#!/usr/bin/perl

open my $in,  '<:raw', 'out.dat' or die $!;
open my $out, '>:raw', 'outfile.jpg' or die $!;
local $/ = \1024;
my $byte;
while (read($in, $byte, 1)) {
	if($byte == x00){
		$byte = chr(x17);}
	elsif($byte == x17){
		$byte = chr(x00);}
	elsif($byte == x4E){
		$byte = chr(x42);}
	elsif($byte == x42){
		$byte = chr(x4E);}
	elsif($byte == x24){
		$byte = chr(x90);}
	elsif($byte == x90){
		$byte = chr(x24);}
	print $out $byte;
}
close $in or die $!;
close $out or die $!;
all it prints is many nulls and sporadic digits 1..9. I've even tried this with an if($byte eq chr(x00)) but it doesn't work eighther.

Replies are listed 'Best First'.
Re^3: Binary conditionals
by moritz (Cardinal) on Sep 30, 2008 at 18:40 UTC
    Always start your scripts with
    use strict; use warnings;

    It catches many of your errors. The correct comparison is either $byte eq "\x00" or $byte eq chr(0x00).

    You can still use a substitution if your mapping is constant:

    my %table = ( "\x00" => "\x17", "\x17" => "\x00", ... ); my $re = join '|', map quotemeta, keys %table; # open in and out files... # and then substitute: $str =~ s/($re)/$table{$1}/g
      That worked perfectly! Thank you!
Re^3: Binary conditionals
by jwkrahn (Abbot) on Sep 30, 2008 at 19:21 UTC

    Since only characters are involved you can use tr///:

    while (<$in>) { tr/\x00\x17\x4E\x42\x24\x90/\x17\x00\x42\x4E\x90\x24/; print $out $_; }