All you really need is an $iif($len(#) == 1,$!!chr(35),#)
Replace "#" with $1 if it's coming from an alias. The OP was sending data via DDE, possibly from outside mIRC, so in that case the check would have to be done from that end, in whatever language is being used.
in a C-like lang: (input == "#" ? "$!chr(35)" : input)