zoukankan      html  css  js  c++  java
  • 工具类

    数据转化

    public class DataConvert
    {

    /// <summary>
    /// 获得MD5加密字符串
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string GetMD5String(string str)
    {
    return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }


    /// <summary>
    /// 获得MD5加密字符串
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string GetFileMD5String(string FilePath)
    {
    if (!File.Exists(FilePath))
    {
    return "";
    }

    try
    {
    using (FileStream get_file = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    System.Security.Cryptography.MD5CryptoServiceProvider get_md5
    = new System.Security.Cryptography.MD5CryptoServiceProvider();
    byte[] hash_byte = get_md5.ComputeHash(get_file);
    string resule = System.BitConverter.ToString(hash_byte);
    resule
    = resule.Replace("-", "");
    get_file.Close();
    get_file.Dispose();
    return resule;
    }
    }
    catch (Exception e)
    {
    Console.WriteLine(e);
    return GetMD5String("");
    }
    }


    /// <summary>
    /// 将对象序列化
    /// </summary>
    /// <param name="info"></param>
    /// <returns>返回UTF8格式编码的字节数组</returns>
    public static byte[] Serialize(object info)
    {
    if (info == null)
    {
    return null;
    }
    JavaScriptSerializer js
    = new JavaScriptSerializer();
    try
    {
    string str = js.Serialize(info);
    if (str == null)
    {
    return null;
    }
    return Encoding.UTF8.GetBytes(str);
    }
    catch (InvalidOperationException ex)
    {
    Console.WriteLine(ex.Message);
    return null;
    }
    }


    /// <summary>
    /// 反序列化
    /// </summary>
    /// <returns></returns>
    public static Dictionary<string, object> Dserialize(string strJsonData)
    {
    if (strJsonData == null || strJsonData == "")
    {
    return null;
    }

    JavaScriptSerializer js
    = new JavaScriptSerializer();
    try
    {
    return js.Deserialize<Dictionary<string, object>>(strJsonData);
    }
    catch (InvalidOperationException ex)
    {
    Console.WriteLine(ex.Message);
    return null;
    }
    catch (ArgumentException exec)
    {
    Console.WriteLine(exec.Message);
    return null;
    }
    }


    /// <summary>
    /// 反序列化
    /// </summary>
    /// <returns></returns>
    public static Dictionary<string, object> Dserialize(byte[] bufferJsonData)
    {
    return Dserialize(Encoding.Default.GetString(bufferJsonData));
    }

    }


    返回导读目录,阅读更多随笔



    分割线,以下为博客签名:

    软件臭虫情未了
    • 编码一分钟
    • 测试十年功


    随笔如有错误或不恰当之处、为希望不误导他人,望大侠们给予批评指正。

  • 相关阅读:
    pickle模块的基本使用
    python selenium 开发环境配置
    ubuntu ftp服务器搭建
    再探VIM配置
    counting elements--codility
    Time complexity--codility
    Arrays--codility
    Iterations --codility
    adobe reader DC 字体设置
    按位操作
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1883708.html
Copyright © 2011-2022 走看看