C # PDU-encoded ucs2 encryption and decryption Function

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.