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

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - 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;
        }
    }