for my $ofs (0..length($str)-1) { my $c=substr($str,$ofs,1); substr($str,$ofs,1)=sprintf("%%%02x",ord($c)) if ord($c)>127 or ord($c)<=32; }