关于IC验证时使用的数据转换总结(C#)

1.将ASCII字符串转为十六进制字节数组()

                 String input = null;
                 input = tbxRecvData.Text.ToString().Replace("\t", "");

                    byte[] readBuffer2 = new byte[(UInt32)System.Text.Encoding.Default.GetBytes(input.ToCharArray()).Length];
                    readBuffer2 = System.Text.Encoding.Default.GetBytes(input);


                    tbxRecvData.Text = "";
                    for (int i = 0; i < (UInt32)System.Text.Encoding.Default.GetBytes(input.ToCharArray()).Length; i++)
                    {
                        //String strA = a.ToString("x8");
                        tbxRecvData.AppendText(readBuffer2[i].ToString("X2") + " ");
                    }

2.十六进制字符串转为ASCII


                String hexValues = tbxRecvData.Text.ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "");

                if (hexValues.Length <= 0)
                    return;

                hexValues = hexValues.Trim();      //去字符串首尾空格的函数
        

                //string hexValues = "48 65 6C 6C 6F 20 57 6F 72 6C 64 21";
                string[] hexValuesSplit = hexValues.Split(' ');

                tbxRecvData.Text = "";

                foreach (String hex in hexValuesSplit)
                {
                    // Convert the number expressed in base-16 to an integer.
                    int value = Convert.ToInt32(hex, 16);
                    // Get the character corresponding to the integral value.
                    string stringValue = Char.ConvertFromUtf32(value);
                    char charValue = (char)value;

                    tbxRecvData.AppendText(stringValue);
                    //Console.WriteLine("hexadecimal value = {0}, int value = {1}, char value = {2} or {3}",
                    //hex, value, stringValue, charValue);
                }

 


        /// <summary> 
        /// 字节数组转16进制字符串 
        /// </summary> 
        /// <param name="bytes"></param> 
        /// <returns></returns> 
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值