123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Bowin.Common.BarCode
- {
- public class BarCodeHelper
- {
- /// <summary>
- /// 判断字符串是否由数字字符组成
- /// </summary>
- /// <param name="input">字符串</param>
- /// <returns>true:由数字字符组成 false:包含其他字符</returns>
- 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;
- }
- /// <summary>
- /// 将原始字符串分隔成字符串数据,判断字符串数组的每一项是否为整形
- /// </summary>
- /// <param name="rawData">需要分隔的原始字符串</param>
- /// <param name="strLength">分隔单位</param>
- /// <returns>true:全部为整形 false:存在一个为非整形</returns>
- 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;
- }
- /// <summary>
- /// 为给定字符串添加后缀(适用UPCA)
- /// </summary>
- /// <param name="rawData">字符串</param>
- 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;
- }
- }
- /// <summary>
- /// 定制异常信息
- /// </summary>
- /// <param name="message">异常信息</param>
- /// <param name="type">异常类型</param>
- public static void ThrowBarCodeException(string message, BarCodeType type)
- {
- BarCodeException exception = new BarCodeException(message);
- exception.BarCodeType = type;
- throw exception;
- }
- }
- }
|