Rather than using the codes that you wrote up, you could've used
Code:
 set %desc $replace($did($dname,2),$chr(32),$chr(95)) 

and likewise to reverse it...I couldn't make a modification of your second code since you use %i in the code, but %i isn't set or evaluated in the code.