物联网:C# Socket发送接收字节数组和十六16进制之间转换函数
发布日期:2021-05-10 10:48:26 浏览次数:16 分类:精选文章

本文共 2191 字,大约阅读时间需要 7 分钟。

������������������������������������������������Socket���������������������������hex������������������������������������DEMO���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������������������������������������������������������������������������������������������������������������������������������

// 16������������������������������
private static byte[] HexStrToByteArray(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
return returnBytes;
}
// ���������������16���������������
public static string ByteToHexString(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
returnStr += bytes[i].ToString("X2");
}
return returnStr;
}

���������ToString("X2") ���C#���������������������������������������������������

  • X���������������������Hex������������
  • 2������������������������������������������������������������������0������������������
  • ���������������������������������������0xA���������������������"XA"������������������������0xA������������"A" - ���������������������������������������������������������������������������������������������������

���������

  • 0xA ������������"A"���������������������������������������������"A"������
  • 0x1A ������������"1A"���
  • ������������������������"0x"���������������������������������������"X2"������"X"���

���������������������������������������������������������������������������������������������������

���������������������������������������������BitConverter.ToString���������

// ���������������
string strHex = BitConverter.ToString(receivedBytes);
strHex = strHex.Replace("-", " ");

������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:mounted与activated的执行顺序
下一篇:SqlDataReade转换成DataTable

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2025年04月18日 19时31分39秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章