$buf = "hello\nworld"; $l = length($buf); for ($x = 0; $x < $l; $x++) { $piece = substr($buf, $x, 1); $i = ord($piece); if ($i =~ m/[ -~]/) { $rhs .= $i; } else { $rhs .= "."; } } #### if ($piece =~ m/[ -~]/)