This looks like it does what you want, and is probably faster to use regsubex than go through a scripted loop. I assume the definition of alnum you're needing is the case-insensitive base36 alphabet. This replicates your "good" example, but I'm not sure either case handles a codepoint in the range 256-4095 which is a 3-digit hex number, or non-alnum in the 33-126 range? Instead of this simplistic substitution pattern, it may need to call a $myalias($asc(\t)) to handle different styles in different ranges. If it needs to be encoding each UTF8 character separately, remove the /u flag.

Code
//tokenize 32 にほんご | var %i 1  | echo -a $regsubex(foo,$1-,/([^0-9A-Za-z])/gu,$chr(37) $+ $base($asc(\t),10,16,2))