using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bowin.Common.BarCode { public class BarCodeHelper { /// /// 判断字符串是否由数字字符组成 /// /// 字符串 /// true:由数字字符组成 false:包含其他字符 public static bool IsNumberic(string input) { int temp = 0; if (!int.TryParse(input, out temp)) { //判断字符串是否为"00121300" foreach (char c in input) { if (!char.IsDigit(c)) { return false; } } } return true; } /// /// 将原始字符串分隔成字符串数据,判断字符串数组的每一项是否为整形 /// /// 需要分隔的原始字符串 /// 分隔单位 /// true:全部为整形 false:存在一个为非整形 public static bool CheckNumericOnly(string rawData, int strLength) { string temp = rawData; string[] strings = new string[(rawData.Length / strLength) + ((rawData.Length % strLength == 0) ? 0 : 1)]; int i = 0; while (i < strings.Length) { if (temp.Length >= strLength) { strings[i++] = temp.Substring(0, strLength); temp = temp.Substring(strLength); } else strings[i++] = temp.Substring(0); } foreach (string s in strings) { long value = 0; if (!long.TryParse(s, out value)) return false; } return true; } /// /// 为给定字符串添加后缀(适用UPCA) /// /// 字符串 private void CheckDigit(ref string rawData) { if (rawData.Length == 0) { throw new ArgumentNullException("参数:rawData不能为空"); } try { string rawDataHolder = rawData.Substring(0, 11); int even = 0; int odd = 0; for (int i = 0; i < rawDataHolder.Length; i++) { if (i % 2 == 0) odd += int.Parse(rawDataHolder.Substring(i, 1)) * 3; else even += int.Parse(rawDataHolder.Substring(i, 1)); } int total = even + odd; int cs = total % 10; cs = 10 - cs; if (cs == 10) cs = 0; rawData = rawDataHolder + cs.ToString()[0]; } catch (FormatException ex) { throw ex; } } /// /// 定制异常信息 /// /// 异常信息 /// 异常类型 public static void ThrowBarCodeException(string message, BarCodeType type) { BarCodeException exception = new BarCodeException(message); exception.BarCodeType = type; throw exception; } } }