【C#|c# 字符串与16进制ASCII码相到转换】1.普通字符串转16进制ASCII码
//普通字符串转16进制ASCII码
public static string toASCII(string code)
{
char[] cs = code.ToCharArray();
//先转字节数组
string Hstr = null;
for (int l = 0;
l < cs.Length;
l++)
{
Hstr += ((int)cs[l]).ToString("X");
}
//System.Console.WriteLine(Hstr);
return Hstr;
}
2.16进制ASCII码转普通字符串
public static string getASCIItoStr(string str)
{
byte[] bb = Hex2Bytes(str, false);
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
string strCharacter = asciiEncoding.GetString(bb);
Console.WriteLine(strCharacter);
return strCharacter;
}
//先将16进制ASCII码转字节数组
public static byte[] Hex2Bytes(string sHex, bool isExchange)
{
if (sHex == null || sHex.Length == 0)
return null;
sHex = sHex.Length % 2 == 0 ? sHex : "0" + sHex;
byte[] bRtns = new byte[sHex.Length / 2];
for (int i = 0;
i < bRtns.Length;
i++)
{
if (isExchange)
bRtns[bRtns.Length - 1 - i] = Convert.ToByte(sHex.Substring(i * 2, 2), 16);
else
bRtns[i] = Convert.ToByte(sHex.Substring(i * 2, 2), 16);
}
return bRtns;
}
}
推荐阅读
- Web前端|【HTML】——基础知识点总结
- C++|【C++】类型转换
- 笔记|C++(继承和派生)
- python|第二十九(如何搭建数据驱动自动化测试框架(粗糙))
- python|Python如何实现人脸识别系统
- Qt入门|Qt QFtp客户端(上传、下载文件)
- qt|Qt_共享内存(QSharedMemory)未完成
- C|【c ++ primer 笔记】第4章 表达式
- C|【c ++ primer 笔记】第3章 字符串、向量和数组