Address: http://www.cnblogs.com/ycdx2001/archive/2011/04/24/2026468.html
/// <Summary>
/// Ucs2 Decoding
/// </Summary>
/// <Param name = "src"> ucs2 source string </param>
/// <Returns> decoded UTF-16BE string </returns>
Public static string decodeucs2 (string SRC)
{
String decucs = SRC. Remove (SRC. indexof ("/R "));
String pstr = "^ [0-9a-fa-f] + $ ";
If (! RegEx. ismatch (decucs, pstr ))
{
Return "invalid string cannot be parsed! ";
}
Stringbuilder builer = new stringbuilder ();
For (INT I = 0; I <decucs. length; I + = 4)
{
Int unicode_nu = int32.parse (decucs. substring (I, 4), system. Globalization. numberstyles. hexnumber );
Builer. append (string. Format ("{0}", (char) unicode_nu ));
}
Return builer. tostring ();
}
/// <Summary>
/// Ucs2 Encoding
/// </Summary>
/// <Param name = "src"> source string of UTF-16BE encoding </param>
/// <Returns> encoded ucs2 string </returns>
Public static string encodeucs2 (string SRC)
{
Stringbuilder builer = new stringbuilder ();
Builer. append ("000800 ");
Byte [] tmpsmstext = encoding. Unicode. getbytes (SRC );
Builer. append (tmpsmstext. length. tostring ("X2"); // body Content Length
For (INT I = 0; I <tmpsmstext. length; I ++ = 2) // high/low bytes
{
Builer. append (tmpsmstext [I + 1]. tostring ("X2"); // convert ("X2") to hexadecimal
Builer. append (tmpsmstext [I]. tostring ("X2 "));
}
Builer = builer. Remove (0, 8 );
Return builer. tostring ();
}