• 工具类


    数据转化

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

    }


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



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

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


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

  • 相关阅读:
    解析CIDR表示的IP段表示的范围
    [Python] 使用乘号复制变量引起的问题
    [Python] 字典dict添加二级键值的问题
    [Java] [刷题] 连续自然数和
    [Java] [刷题] 多个整数连接为最大整数问题
    [CentOS] 编译安装Python3后pip3安装的库如何在命令行调用
    [CentOS] 宝塔面板与Python3的恩怨情仇
    [易语言] 两种字节序的直观比较
    [Java] [刷题] Excel地址转换
    [Java] 运算精度
  • 原文地址:https://www.cnblogs.com/08shiyan/p/1883708.html
Copyright © 2020-2023  润新知