zoukankan
html css js c++ java
一个代码转换的类
/**/
///
<summary>
///
代码转换功能
///
</summary>
public
class
CodingChange
{
/**/
///
<summary>
///
把字符型转换成16进制编码
///
</summary>
///
<param name="character">
字符串
</param>
///
<returns>
一个字符转成4位编码
</returns>
public
static
string
CharacterToCoding(
string
character)
{
string
coding
=
""
;
for
(
int
i
=
0
; i
<
character.Length; i
++
)
{
//
取出二进制编码内容
byte
[] bytes
=
System.Text.Encoding.Unicode.GetBytes(character.Substring(i,
1
));
//
取出低字节编码内容(两位16进制)
string
lowCode
=
System.Convert.ToString(bytes[
0
],
16
);
if
(lowCode.Length
==
1
)
lowCode
=
"
0
"
+
lowCode;
//
取出高字节编码内容(两位16进制)
string
hightCode
=
System.Convert.ToString(bytes[
1
],
16
);
if
(hightCode.Length
==
1
)
hightCode
=
"
0
"
+
hightCode;
coding
+=
(lowCode
+
hightCode);
//
加入到字符串中
}
return
coding;
}
/**/
///
<summary>
///
把16进制编码转换成字符型
///
</summary>
///
<param name="coding">
4位转成一位,长度必须是4的倍数
</param>
///
<returns>
字符串
</returns>
public
static
string
CodingToCharacter(
string
coding)
{
string
characters
=
""
;
if
(coding.Length
%
4
!=
0
)
//
编码为16进制,必须为4的倍数。
{
throw
new
System.Exception(
"
编码格式不正确
"
);
}
for
(
int
i
=
0
; i
<
coding.Length; i
+=
4
)
//
每四位为一个汉字
{
byte
[] bytes
=
new
byte
[
2
];
string
lowCode
=
coding.Substring(i,
2
);
//
取出低字节,并以16进制进制转换
bytes[
0
]
=
System.Convert.ToByte(lowCode,
16
);
string
highCode
=
coding.Substring(i
+
2
,
2
);
//
取出高字节,并以16进制进行转换
bytes[
1
]
=
System.Convert.ToByte(highCode,
16
);
string
character
=
System.Text.Encoding.Unicode.GetString(bytes);
characters
+=
character;
}
return
characters;
}
}
查看全文
相关阅读:
什么是前后端分离?
Ubuntu修改时区和更新时间
待学
Pycharm默认输入状态是insert状态,选中文字无法直接输入替换或删除
使用jsonify返回json数据
Linux
Linux
Linux
JavaScript
JavaScript
原文地址:https://www.cnblogs.com/liubiqu/p/66863.html
最新文章
python---一个非常简单好用的图形界面库--PySimpleGUI
Python----分布式进程使用(Queue和BaseManager使用)---(搬运)
python自带库--multiprocessing库
python第三方库---BeautifulSoup库(搬运)
python自带库---re库
第二部分数组和引用(基于堆栈的计算器实现)(C++ In Action 学习总结)
SQLAlchemy常用功能的简单总结(来自于官方文档)
Docker安装elasticsearch和kibana脚本(解决内存不够导致的启动不了es)
ASP.NET Core 中 国内邮箱(163邮箱) 帐户确认和密码恢复
自定义Mongodb的操作接口
热门文章
Tornado(1) 上传图片并显示
关于python函数参数的传递*args, **kwargs的区别及用法(实例)
Python爬取新闻列表并保存为xml(读取ini配置文件)
configparser读取ini配置文件&&xml的读写
使用selenium爬取小说以及一些注意事项和坑
Python的内置函数,super()超类用法解析
Django测试开发-15-django.utils.encoding未发现 python_2_unicode_compatible包
CentOS7环境下 安装虚拟环境 virtualenvwrapper
在Centos,Ubuntu,Windows下安装virtualenv
Django FBV/CBV接口开发方式
Copyright © 2011-2022 走看看