Ruby, 41 35 34 3234 characters
(40 34 33 3133 characters code + 1 character command line option)
gsub(/(.*?)#/){putc$1.size%127}
gsub(/.*?#/){putc$&.count ?;%127}
Thanks to:
- Jordan for suggesting to use
putc
to not need explicit conversion with.chr
(6 characters) - Kirill L. for finding the unnecessary parenthesis (1 character)
- Jordan for using captured group to be able to change from
.count
to.size
(2 characters)
Sample run:
bash-54.1$4$ ruby -ne 'gsub(/(.*?)#/){putc$1putc$&.size%127count ?;%127}' < '2d{.;#' | od -tad1
0000000 2 fsetb d nul nul etxnul sohnul stx stx{ nul nul nul
50 2823 100 0 0 3 0 1 0 2123 20 0 0
0000013