c#一个简单例子

(大神直接掠过)
这是一个输入任何字符串,都会按照规定返回的类。我很无语
如果考虑到两个汉字和4个英文字母的宽度一致的话,我们大概可以先将string 转化为比特流。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
    class Program
    {
        public static string tempStr1 = "中国人哦中国人哦中国人哦中国人哦中国人哦中国人哦";
        static void Main(string[] args)
        {
            string tempStrMain = GetStringYong(tempStr1);
            Console.Write(tempStrMain);
            Console.Read();
        }
        public static string GetStringYong(string value)
        {
            int tempStrLength = value.Length;
            StringBuilder outputStr = new StringBuilder();
            for (int i = 0; i < tempStrLength; i++)
            {
                if ((i + 1) % 4 == 0 || i == 0)
                {
                    if ((i + 1) % 12 == 0)
                    {
                        //outputStr.AppendLine("/r/n");
                        outputStr.Append("\r\n");
                        outputStr.Append(GetCharYong(i, 4, tempStrLength, value) + "\0\0\0");
                    }
                    else
                    {
                        outputStr.Append(GetCharYong(i, 4, tempStrLength, value) + "\0\0\0");
                    }
                }
            }
            return outputStr.ToString();
        }
        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="index"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetCharYong(int index, int length, int tempStrLength,string tempStr)
        {
            try
            {
                if (!(index == 0) && tempStrLength >= 4)
                {
                    index += 1;
                }
                if (tempStrLength < 4)
                {
                    return tempStr;
                }
                if (index == tempStrLength)
                {
                    return "结束";
                }
                if (tempStrLength < index + length)
                {
                    string ssTemp;
                    int lengthNew = tempStrLength - index;
                    ssTemp = tempStr.Substring(index, lengthNew);
                    return ssTemp;
                }
                string ss;
                ss = tempStr.Substring(index, length);
                return ss;
            }
            catch (Exception)
            {
                return "Exception";
                throw;
            }
        }
    }
}



还有就是 ,串口通信的校验位1.5bit,搞不清楚,头晕。

猜你喜欢

转载自blog.csdn.net/qq_37326058/article/details/89247451