将字符串生成ZPL的Code128Auto编码

        internal string Code128AutoZPL(string planeText) {
            StringBuilder sb1 = new StringBuilder();
            bool isDigit = GetDigitLength(planeText, 0) >= 2;
            for (int i = 0; i < planeText.Length; i++) {
                int len = GetDigitLength(planeText, i);
                len = len / 2 * 2;
                if (i == 0) {
                    if (len >= 2) {
                        if (isDigit) sb1.Append(">;"); //   开头连续2位数字 分号
                        sb1.Append(planeText.Substring(i, len));
                        i += (len - 1);
                        isDigit = true;
                    } else {
                        if (!isDigit) sb1.Append(">:"); //   开头字母 冒号
                        sb1.Append(planeText.Substring(i, 1));
                        isDigit = false;
                    }
                } else {
                    if (len >= 4) {
                        if (!isDigit) sb1.Append(">5"); //   连续4位数字
                        sb1.Append(planeText.Substring(i, len));
                        i += (len - 1);
                        isDigit = true;
                    } else {
                        if (isDigit) sb1.Append(">6"); //    字母
                        sb1.Append(planeText.Substring(i, 1));
                        isDigit = false;
                    }
                }
            }
            return sb1.ToString();
        }
internal int GetDigitLength(string data, int startIndex) { //默认设定从起始位置开始至最后都是数字 int digitLength = data.Length - startIndex; for (int i = startIndex; i < data.Length; i++) { if (!char.IsDigit(data[i])) { digitLength = i - startIndex; break; } } return digitLength; }

将字符串生成ZPL的Code128Auto编码。

posted @ 2022-09-03 17:50  enif  阅读(213)  评论(0编辑  收藏  举报
豫ICP备2021034901号