Ah, I understand. Your goal is to encode Unicode text in HTML which is itself encoded in ASCII only, so it can be transmitted through channels (such as bad editors) which mess up non-ASCII text.
To encode any Unicode text this way we'd also want to encode HTML special characters such as <, >, and &; but that could be done before calling your routine.