正则表达式+编码转换小工具
下午花了点时间写了个正则表达式和编码转换的工具,正则表达式工具是模仿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;
{
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;
{
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("-", " ");
}
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, null, out vBytes[i / 2]))
vBytes[i / 2] = 0;
return ASCIIEncoding.Default.GetString(vBytes);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
{
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, null, out vBytes[i / 2]))
vBytes[i / 2] = 0;
return ASCIIEncoding.Default.GetString(vBytes);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return "";
}
}
喜欢的拿去,
已经附上源码 。。。
每天进步一点点...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现