I needed to encode strings in a reversible way to be valid as XHTML IDs (letters, numbers, '-' and '_', must start with a letter). The following encode and decode functions do just that. If you are not using Drupal just replace drupal_substr() with your favourite multi-byte safe substr() function.
* Encode a string for use as a DOM id.
* Replaces non-alphanumeric characters with an underscore and the hex representation of the character code
* with letters in lowercase