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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
H
Hacker News: Front Page
Hacker News - Newest:
Hacker News - Newest: "LLM"
L
Lohrmann on Cybersecurity
Cisco Talos Blog
Cisco Talos Blog
O
OpenAI News
S
Securelist
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
H
Heimdal Security Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recorded Future
Recorded Future
Microsoft Azure Blog
Microsoft Azure Blog
MyScale Blog
MyScale Blog
Webroot Blog
Webroot Blog
The Hacker News
The Hacker News
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
N
Netflix TechBlog - Medium
N
News and Events Feed by Topic
D
Docker
D
DataBreaches.Net
A
About on SuperTechFans
T
Tor Project blog
V
V2EX
G
Google Developers Blog
博客园 - Franky
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
I
InfoQ
H
Help Net Security
V2EX - 技术
V2EX - 技术
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
SecWiki News
SecWiki News
The Register - Security
The Register - Security
人人都是产品经理
人人都是产品经理
NISL@THU
NISL@THU
小众软件
小众软件
B
Blog
T
Threatpost
P
Palo Alto Networks Blog
博客园 - 【当耐特】
L
LangChain Blog
AWS News Blog
AWS News Blog
月光博客
月光博客
宝玉的分享
宝玉的分享

博客园 - hu晓峰

记winform程序异常排查 记一次wpf 背景图的坑点 【Unity踩坑】Unity项目管理员权限问题(Unity is running as administrator ) 依赖注入 微服务聚合查询 libmodbus编译为64位动态库 一文读懂Modbus协议:工业设备的“普通话“通信指南 Mysql union与union all有什么区别? 理解Systemd服务重启策略:on-failure vs always Redis分布式锁正确的实现方法 C# 解决串口通讯中,返回数据不完整 字典Dictionary.Add不是把新的元素插入到字典最后面 c# Avalonia 架构开发跨平台应用 ‌索引基数 MySQL InnoDB损坏修复:使用innodb_force_recovery 整数取低字节 avalonia在linux下运行出现Default font family name can't be null or empty问题的解决 ICMP timestamp请求响应漏洞CVE-1999-0524解决方法 详解mysql的for update 使用Redis的SETNX命令实现分布式锁 ASP.NET Core中如何对不同类型的用户进行区别限流
C#汉字-区位码相互转化类
hu晓峰 · 2024-12-13 · via 博客园 - hu晓峰

项目中需要对汉字使用区位码进行转化,写了一个类,分享如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Rare.Card.Libary.Helper
{
    public class CharacterAreaCodingConvertHelper
    {
        /// <summary>
        /// 汉字转区位码
        /// </summary>
        /// <param name="character"></param>
        /// <returns></returns>
        public static string CharacterToCoding(string character)
        {
            string coding = string.Empty;
            for (int i = 0; i < character.Length; i++)
            {
                byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(character.Substring(i, 1));
                string lowCode = System.Convert.ToString(bytes[0], 16); //取出低字节编码内容(两位16进制)
                if (lowCode.Length == 1)
                    lowCode = "0" + lowCode;
                string hightCode = System.Convert.ToString(bytes[1], 16);//取出高字节编码内容(两位16进制)
                if (hightCode.Length == 1)
                    hightCode = "0" + hightCode;
                coding += (lowCode + hightCode);//加入到字符串中,
            }
            return coding;
        }
        /// <summary>
        /// 区位码取汉字
        /// </summary>
        /// <param name="coding"></param>
        /// <returns></returns>
        public static string CodingToCharacter(string coding)
        {
            string characters = string.Empty;
            if (coding.Length % 4 != 0)//编码为16进制,必须为4的倍数。
            {
                throw new System.Exception("编码格式不正确");
            }
            for (int i = 0; i < coding.Length / 4; i++)
            {
                byte[] bytes = new byte[2];
                int j = i * 4;
                string lowCode = coding.Substring(j, 2); //取出低字节,并以16进制进制转换
                bytes[0] = System.Convert.ToByte(lowCode, 16);
                string highCode = coding.Substring(j + 2, 2); //取出高字节,并以16进制进行转换
                bytes[1] = System.Convert.ToByte(highCode, 16);
                string character = System.Text.Encoding.GetEncoding("GB2312").GetString(bytes);
                characters += character;
            }
            return characters;
        }
    }
}

测试数据:

比如“布”
 查出来的是1828
 18---0x12 -----0x12+0xa0 = 0xB2
 28 ---- 0x1c + 0xA0 ------0xBC
 所以“布”的区位码是B2Bc
字符串:张三的区位码转化以后是D5C5C8FD