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;
}
}
查看全文
相关阅读:
进制转化
8.28总结前日及今日
Uva1213(线性筛模板+dp)
Uva1363(余数性质/减少枚举量)
Uva1640(统计数字出现的次数)
Uva1639(概率期望/对数处理避免丢失精度)
Uva12034 (组合数取模)
Uva10820 欧拉公式模板(求小于n且与n互素的数的个数)
Uva1635 二项式递推+质因子分解+整数因子分解
8.19总结今日
原文地址:https://www.cnblogs.com/liubiqu/p/66863.html
最新文章
在ListCtrl中进行排序(二)
CListCTrl控件排序(一)
C++ 新建进程和结束进程
MFC 对话框Picture Control(图片控件)中静态和动态显示Bmp图片
KERNEL_DATA_INPAGE_ERROR 蓝屏错误
MFC 加载链接库(DLL)错误
通过CImageList加载图标 报错
Npoi导入时:Found EOFRecord before WindowTwoRecord was encountered
用户 'sa' 登录失败。 (Microsoft SQL Server,错误: 18456)
未找到与约束contractname Microsoft.VisualStudio.Utilities.IContentTypeRegistryService
热门文章
各种工具的下载地址
webserver引用出现”服务器未能识别 HTTP 头 SOAPAction 的值
SQL Server--通过存储过程生成表数据的脚本
SQL Server--通过存储过程生成表结构的脚本
通过文件名获取文件类型ContentType
asp.net出现乱码的解决办法
report Viewer从V10.0升级到V11.0全过程
我的python学习之路-文件/函数/迭代器/生成器/推导式/高阶函数
我的python学习之路-容器类型数据操作
我的Python学习之路-数据类型/运算符
Copyright © 2011-2022 走看看