HOW TO Convert string to charactor code RRS feed

  • Question

  • User1751330032 posted


    I am working on a project which need to support multi-languages, and i need to convert the user input string into char code in order to display them, I couldn't find any build-in function in c#, so i wrote one my own, but i am still hoping to have something build-in to do this, any idea? here's the code i wrote:

    public string ToCharCode(string srcStr)
                StringBuilder sbCharCode = new StringBuilder();
                char[] srcChar = srcStr.ToCharArray();

                for (int i = 0; i < srcChar.Length; i++)
                    Int32 charCode = Convert.ToInt32(+srcChar[i]);
                    if (charCode < 128)
                        sbCharCode.Append("&#" + Convert.ToInt32(+srcChar[i]).ToString() + ";");
                return sbCharCode.ToString();



    Monday, November 13, 2006 4:26 AM


  • User1416329745 posted

    Here is a little trick that may work the .NET Char is the nineth integer and Unicode by default so if you write your Chinese pages first and save them as Unicode 16 it will be easy to save your none Chinese imput as Unicode 8.  The problem maybe with the database where the Nchar and Nvarchar are not the same with the .NET Char untill 125 so do what the Asp.net team do make name Nvarchar 150 and above.  The reason is both types will be the same, it is not a complete solution but you get the idea trick the type system.  Post again if you still have questions, and read the link below about encoding.  Hope this helps.


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 13, 2006 11:25 PM