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

推荐订阅源

P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
腾讯CDC
人人都是产品经理
人人都是产品经理
小众软件
小众软件
V
Visual Studio Blog
S
Secure Thoughts
J
Java Code Geeks
V
V2EX
量子位
The Hacker News
The Hacker News
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
T
Tailwind CSS Blog
Cloudbric
Cloudbric
S
SegmentFault 最新的问题
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Application and Cybersecurity Blog
Application and Cybersecurity Blog
IT之家
IT之家
T
Tenable Blog
S
Security @ Cisco Blogs
月光博客
月光博客
雷峰网
雷峰网
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
C
Cybersecurity and Infrastructure Security Agency CISA
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
Attack and Defense Labs
Attack and Defense Labs
博客园 - 三生石上(FineUI控件)
Hacker News - Newest:
Hacker News - Newest: "LLM"
有赞技术团队
有赞技术团队
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Cloudflare Blog
K
Kaspersky official blog

博客园 - 亦心

教你如何查找并下载某手某音上的外部视频 求出两个字符串中最大长度的相同的子字符串 年会抽奖软件 sql语句跨库导入导出 Google maps API开发(二) - 亦心 Google maps API开发(一) - 亦心 Javascript树型菜单(含源码) Javascript结合XML省市级联 - 亦心 - 博客园 ASP.NET程序读取二代身份证(附源码) - 亦心 - 博客园 仿百度搜索智能提示(纯JS实现) 发现一个免费申请国际域名的地方!! asp.net性能优化 分享一个分页存储过程和分页函数 利用反射实现通用的DataReader转List、DataReader转实体类 任意类型转换成json tsql字符串操作 javascript正则表达式 - 亦心 - 博客园 access、excel取随机n条记录 测试SQL Server执行时间和CPU时间
正则表达式+编码转换小工具
亦心 · 2012-09-13 · via 博客园 - 亦心


 


下午花了点时间写了个正则表达式和编码转换的工具,正则表达式工具是模仿YART Yet Another Regex Tester写的,而里面

涉及到编码转换的核心算法如下,工具在下面可以下载。

1、unicode编码转汉字

核心算法:

        

public string StrToGB (string s)
        {
            if (s.Length > 0)
            {
                MatchCollection mc = Regex.Matches(s, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                foreach (Match m in mc)
                {
                    byte[] buffer = new byte[2];
                    string r = string.Empty;
                    string m0 = m.Groups[0].Value;
                    string m1 = m.Groups[1].Value;
                    string m2 = m.Groups[2].Value;
                    buffer[0] = (byte)int.Parse(m2, NumberStyles.HexNumber);
                    buffer[1] = (byte)int.Parse(m1, NumberStyles.HexNumber);
                    r += Encoding.Unicode.GetString(buffer);
                    //\\u8f6c\\u53d1
                    
//\\u8bc4\\u8bba
                    s = s.Replace(m0, r);
                }
            }
            return s;

         }

2、汉字转unicode

        

public string GBToUnicode(string text)
        {
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
            string lowCode = "", temp = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                if (i % 2 == 0)
                {
                    temp = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容(两位16进制)
                    if (temp.Length < 2) temp = "0" + temp;
                }
                else
                {
                    string mytemp = Convert.ToString(bytes[i], 16);
                    if (mytemp.Length < 2) mytemp = "0" + mytemp;
                    lowCode = lowCode + @"\u" + mytemp + temp;//取出元素4编码内容(两位16进制)
                }
            }
            return lowCode;

           }

3、字符转16进制

      public string StrToHex(string mStr) //返回处理后的十六进制字符串 

   {
            return BitConverter.ToString(ASCIIEncoding.Default.GetBytes(mStr)).Replace("-"" ");
   }

4、16进制转字符

        

public string HexToStr(string mHex) // 返回十六进制代表的字符串 
        {
            try
            {
                mHex = mHex.Replace(" """);
                mHex = mHex.Replace("-""");
                if (mHex.Length <= 0)
                    return "";
                byte[] vBytes = new byte[mHex.Length / 2];
                for (int i = 0; i < mHex.Length; i += 2)
                    if (!byte.TryParse(mHex.Substring(i, 2), NumberStyles.HexNumber, nullout vBytes[i / 2]))
                        vBytes[i / 2] = 0;
               
                return ASCIIEncoding.Default.GetString(vBytes);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return "";
            }

喜欢的拿去,

已经附上源码 。。。

/Files/cyan/WindowsFormsApplicationUnicode_Source.rar

/Files/cyan/WindowsFormsApplicationUnicode_Release.rar 

每天进步一点点...