1. 程式人生 > >C#字串和十六進位制之間的轉換方法

C#字串和十六進位制之間的轉換方法

/// <summary>
/// <函式:Encode>
/// 作用:將字串內容轉化為16進位制資料編碼,其逆過程是Decode
/// 引數說明:
/// strEncode 需要轉化的原始字串
/// 轉換的過程是直接把字元轉換成Unicode字元,比如數字"3"-->0033,漢字"我"-->U+6211
/// 函式decode的過程是encode的逆過程.
/// </summary>
/// <param name="strEncode"></param>
/// <returns></returns>
public static string Encode(string strEncode)
{
    string strReturn = "";//  儲存轉換後的編碼
    foreach (short shortx in strEncode.ToCharArray())
    {
        strReturn += shortx.ToString("X4");
    }
    return strReturn;
}