在学习数据解析的时候遇到了问题,记录了下来
总的来说就是怎么转换的就得怎么去解析。
1.10进制和16进制的转换
10进制转换为16进制
s.tostring("X");
16进制转换为10进制
convert.tostring(0xa,10);
2.16进制转换为字符串
public string ByteToString(byte[] sendByte)
{
string str_data = null;
for (int i = 0; i < sendByte.Length; i++)
{
str_data += (sendByte[i].ToString("X2") + " ");
}
return str_data.Trim();
}
3.string转换为字节
如果是单个转换为字节,
convert.tobyte();
如果是多个字节
bitconverter.getbytes();
如果转换为ascii吗
encoding.ascii.getbytes();
4.字节解析出输出
单个解析
public string ByteToText(byte[] send)
{
for (int i = 0; i < send.Length; i++)
{
result += Convert.ToString(send[i], 10) + ".";
}
return result;
}
多个字节解析
bitconverter.toint16();
ASCII码
enconding.ascii.getstring();