【发布时间】:2009-05-15 03:33:45
【问题描述】:
我需要对文本文件进行字节移位。我对 perl 一无所知,但我在 perl 中发现了一段完美的代码,称为 moz-byteshift.pl (documentation)。这正是我想做的,但我需要在 C# 中完成。
这里是perl文件的源代码:
#!/usr/bin/perl
# To perform a byteshift of 7
# To decode: moz-byteshift.pl -s -7 <infile >outfile
# To encode: moz-byteshift.pl -s 7 <infile >outfile
# To perform a byteshift of 13
# To decode: moz-byteshift.pl -s -13 <infile >outfile
# To encode: moz-byteshift.pl -s 13 <infile >outfile
use encoding 'latin1';
use strict;
use Getopt::Std;
use vars qw/$opt_s/;
getopts("s:");
if(!defined $opt_s) {
die "Missing shift\n";
}
my $buffer;
while(1) {
binmode(STDIN, ":raw");
my $n=sysread STDIN, $buffer, 1;
if($n == 0) {
last;
}
my $byte = unpack("c", $buffer);
$byte += 512 + $opt_s;
$buffer = pack("c", $byte);
binmode(STDOUT, ":raw");
syswrite STDOUT, $buffer, 1;
}
如果有人至少能解释一下 perl 脚本是如何工作的,那就太好了。 C# 中等效的示例代码会更好。 =)
感谢您的帮助。
【问题讨论】:
-
我不明白。如果,正如您在一条评论中所说,您实际上并不知道 perl 脚本的作用,那么您怎么知道这是您想要做的?
-
一个同事正在使用这个脚本来执行我现在必须实现的功能。就是这样。
标签: c# perl byte-shifting