1.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;
}
/// <summary>
/// <函數:Decode>
///作用:將16進制數據編碼轉化為字符串,是Encode的逆過程
/// </summary>
/// <param name="strDecode"></param>
/// <returns></returns>
public static string Decode(string strDecode)
{
string sResult = "";
for (int i = 0; i < strDecode.Length / 4; i++)
{
sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);
}
return sResult;
}
2.VB6中的代碼
'*******************************************************************
'<函數:Encode>
'作用:將字符串內容轉化為16進制數據編碼,其逆過程是Decode
'參數說明:
'strSource 需要轉化的原始字符串
Public Function Encode(strEncode As String) As String
Dim i As Long
Dim chrTmp$
Dim ByteLower$, ByteUpper$
Dim strReturn$ '存儲轉換後的編碼
For i = 1 To Len(strEncode)
chrTmp$ = Mid(strEncode, i, 1)
ByteLower$ = Hex$(AscB(MidB$(chrTmp$, 1, 1)))
If Len(ByteLower$) = 1 Then ByteLower$ = "0" & ByteLower$
ByteUpper$ = Hex$(AscB(MidB$(chrTmp$, 2, 1)))
If Len(ByteUpper$) = 1 Then ByteUpper$ = "0" & ByteUpper$
strReturn$ = strReturn$ & ByteUpper$ & ByteLower$
Next
Encode = strReturn$
End Function
'</函數:Encode>