bytes转string

代码实现


    /// <summary>
    /// bytes 转 string
    /// </summary>
    static class HexToString
    {

        public static string ToHexString(byte[] bytes)
        {
            return ToHexString(bytes, true);
        }
        public static string ToHexString(byte[] bytes, bool space)
        {
            return ToHexString(bytes, bytes.Length, space);
        }
        public static string ToHexString(byte[] bytes, int length, bool space)
        {
            string strFill = space ? " " : "";
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();

                for (int i = 0; i < length; i++)
                {
                    strB.Append(bytes[i].ToString("X2") + strFill);
                }
                hexString = strB.ToString();
            }

            hexString = hexString.Trim();

            return hexString;
        }
    }

调用


        private void button4_Click(object sender, EventArgs e)
        {
            byte[] bytes = new byte[4];
            bytes[0] = 0xab;
            bytes[1] = 0x02;
            bytes[2] = 0x34;
            bytes[3] = 0x56;

            richTextBox1.AppendText(HexToString.ToHexString(bytes));

        }

参考链接

一文看懂 CAN 通信:C# 实现上位机通信方法https://mp.weixin.qq.com/s?__biz=MzA4MTQyNDk4OA==&mid=2456972237&idx=2&sn=40c150b4502a30f1bd8925c780783802&chksm=893c847228473cce89cc71c67b005a40de3d75fe8edbd53be6570c4d7fac0982620786107036&mpshare=1&scene=1&srcid=0613ccpJxas4hZQVStKDgKi8&sharer_shareinfo=a46ba7747df27d95c2e102699589edab&sharer_shareinfo_first=a46ba7747df27d95c2e102699589edab&exportkey=n_ChQIAhIQuzz0kvmwOTL0GpHluUROThKfAgIE97dBBAEAAAAAAI5oDJ0tJ90AAAAOpnltbLcz9gKNyK89dVj0bHimONySqQwa%2BfLk%2FrxT2wQGS%2FW4WyDxxMCX0ZDIm0jUvN0SttPcyOIUgko6ntLREb1oqQyoME9OUw6MzBZ3V08Ob9p1C%2BMf403d%2FcKO%2FxvpNXzeEUJh6Bv4lYN3vOkFlPuCUNJXkOTy1Bg46h9BmnGO2EFlnhKpEUbkd%2B6bxBYS7k3G%2FxZ0YITD4DJgiEQZtHJrdWe%2FdhLOPYeZzHJ4jLURd4a3zVyFnI%2F2jxENBOLP0JIgyur5xeZVumZqVB9yrpSza9MXHrSXt4syrQJm8aySChs9KVDqNMLHCu67m9YbNeZQF2j7sWjXa5IZT%2BXVKY1fnPw3fK8Z&acctmode=0&pass_ticket=EmOW%2BqgROarUO8lR8PeN%2BA25gtkrvoWUNnUaLg8%2BWw519t8O2curhOua5e%2BLfIRn&wx_header=0#rd

特此记录

anlog

2025年6月13日

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值