I think that a global convert is better solution than char by char replacement. I have tested this code and works fine in all my tests.

If you prefer I can do it char by char, but I think that is not the best way to do it.