zoukankan      html  css  js  c++  java
  • C#PDU编码UCS2加密解密函数

    原文地址:http://www.cnblogs.com/ycdx2001/archive/2011/04/24/2026468.html

           /// <summary>
           
    /// UCS2解码 
           
    /// </summary>

           
    /// <param name="src"> UCS2 源串 </param>
           
    /// <returns> 解码后的UTF-16BE字符串</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 "非法字符串无法解析!"
    ;
               }

               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编码
            
    /// </summary>

            
    /// <param name="src"> UTF-16BE编码的源串</param>
            
    /// <returns>编码后的UCS2串 </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")); //正文内容长度

                for (int i = 0; i < tmpSmsText.Length; i += 2//高低字节对调 
                {
                    builer.Append(tmpSmsText[i 
    + 1].ToString("X2"));//("X2")转为16进制

                    builer.Append(tmpSmsText[i].ToString("X2"));
                }
                builer 
    = builer.Remove(08
    );

                
    return
     builer.ToString();
            }  

  • 相关阅读:
    centos 安装nginx + 多个tomcat负载均衡
    centos 安装redis2.8.9
    centos下彻底删除mysql
    List的深度序列化Demo
    session失效刷新后登录页面嵌入在iframe中的解决办法
    Unable to execute dex: Multiple dex files define 解决方法
    Android运行时异常“Binary XML file line # : Error inflating class”
    在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
    ViewStub的使用示例
    android中导入低版本project可能会遇到的编译问题
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306774.html
Copyright © 2011-2022 走看看