惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
Vercel News
Vercel News
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
P
Proofpoint News Feed
量子位
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
博客园 - 【当耐特】
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
IT之家
IT之家
T
The Blog of Author Tim Ferriss
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)

博客园 - tiasys

xml序列化与反序列化工具 Win10 15063 开始运行不保存历史记录原因和解决方法 win10 localhost 解析为 ipv6地址 ::1 的解决办法 在VisualStudio中应该使用什么字体 S7-200系列PLC与WINCC以太网通信CP243i的实例 超棒的 15 款 Bootstrap UI 编辑器 NuGet学习笔记(转) SQLServer公历转农历函数(1900年-2049年) WPF 的datagrid 列名中没有显示下划线是怎么回事? Vmware9.0打开早期版本报错:this virtual machine’s policies are too old to be run by this version of vmware workstation” Remove WebCakeDesktop Windows8[启用WCF IIS8 svc文件访问服务] Windows8[启用IIS8 asp.net功能] Windows8 [未启用.net3.5功能] Windows8[Web应用程序项目***已配置为使用IIS。无法访问IIS元数据库,您没有足够的特权访问计算机上的IIS网站] Oracle 正确删除archivelog文件 SQLSERVER 删除重复记录 .NET实现Wap飞信协议 黑链代码
guid与Base64编码互相转换
tiasys · 2018-08-05 · via 博客园 - tiasys

guid的长度比较长,本文提供一种方法,将guid转为base64字符串,只有22位长度,比较好!

参考:https://blog.csdn.net/tgghfbflishuai/article/details/17039751

 /// <summary>
    /// Guid转换类
    /// </summary>
   public class GuidConvert
    {
        /// <summary>
        /// guid转为Base64编码
        /// 长度为22个字符
        /// </summary>
        /// <param name="guid"></param>
        /// <example>
        /// string guid2 = GuidToBase64String(guid1);
        /// </example>
        /// <returns></returns>
        public static string GuidToBase64String(Guid guid)
        {
            return Convert.ToBase64String(guid.ToByteArray()).Replace("/", "_").Replace("+", "-").Substring(0, 22);
        }

        /// <summary>
        /// 将Base64编码的guid还原为guid
        /// base64编码字符不包含最后的“==”
        /// </summary>
        /// <param name="target"></param>
        /// <example>
        /// Guid guid3 = Base64StringToGuid(guid2);
        /// </example>
        /// <returns></returns>
        public static Guid Base64StringToGuid(string base64string)
        {
            Guid guid = Guid.Empty;
            if ((!string.IsNullOrEmpty(base64string)) && (base64string.Trim().Length == 22))
            {
                string encoded = string.Concat(base64string.Trim().Replace("-", "+").Replace("_", "/"), "==");
                try
                {
                    byte[] base64 = Convert.FromBase64String(encoded);
                    guid = new Guid(base64);
                }
                catch (FormatException)
                {
                }
            }
            return guid;
        }
    }