BarCodeHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Bowin.Common.BarCode
  6. {
  7. public class BarCodeHelper
  8. {
  9. /// <summary>
  10. /// 判断字符串是否由数字字符组成
  11. /// </summary>
  12. /// <param name="input">字符串</param>
  13. /// <returns>true:由数字字符组成 false:包含其他字符</returns>
  14. public static bool IsNumberic(string input)
  15. {
  16. int temp = 0;
  17. if (!int.TryParse(input, out temp))
  18. {
  19. //判断字符串是否为"00121300"
  20. foreach (char c in input)
  21. {
  22. if (!char.IsDigit(c))
  23. {
  24. return false;
  25. }
  26. }
  27. }
  28. return true;
  29. }
  30. /// <summary>
  31. /// 将原始字符串分隔成字符串数据,判断字符串数组的每一项是否为整形
  32. /// </summary>
  33. /// <param name="rawData">需要分隔的原始字符串</param>
  34. /// <param name="strLength">分隔单位</param>
  35. /// <returns>true:全部为整形 false:存在一个为非整形</returns>
  36. public static bool CheckNumericOnly(string rawData, int strLength)
  37. {
  38. string temp = rawData;
  39. string[] strings = new string[(rawData.Length / strLength) + ((rawData.Length % strLength == 0) ? 0 : 1)];
  40. int i = 0;
  41. while (i < strings.Length)
  42. {
  43. if (temp.Length >= strLength)
  44. {
  45. strings[i++] = temp.Substring(0, strLength);
  46. temp = temp.Substring(strLength);
  47. }
  48. else
  49. strings[i++] = temp.Substring(0);
  50. }
  51. foreach (string s in strings)
  52. {
  53. long value = 0;
  54. if (!long.TryParse(s, out value))
  55. return false;
  56. }
  57. return true;
  58. }
  59. /// <summary>
  60. /// 为给定字符串添加后缀(适用UPCA)
  61. /// </summary>
  62. /// <param name="rawData">字符串</param>
  63. private void CheckDigit(ref string rawData)
  64. {
  65. if (rawData.Length == 0)
  66. {
  67. throw new ArgumentNullException("参数:rawData不能为空");
  68. }
  69. try
  70. {
  71. string rawDataHolder = rawData.Substring(0, 11);
  72. int even = 0;
  73. int odd = 0;
  74. for (int i = 0; i < rawDataHolder.Length; i++)
  75. {
  76. if (i % 2 == 0)
  77. odd += int.Parse(rawDataHolder.Substring(i, 1)) * 3;
  78. else
  79. even += int.Parse(rawDataHolder.Substring(i, 1));
  80. }
  81. int total = even + odd;
  82. int cs = total % 10;
  83. cs = 10 - cs;
  84. if (cs == 10)
  85. cs = 0;
  86. rawData = rawDataHolder + cs.ToString()[0];
  87. }
  88. catch (FormatException ex)
  89. {
  90. throw ex;
  91. }
  92. }
  93. /// <summary>
  94. /// 定制异常信息
  95. /// </summary>
  96. /// <param name="message">异常信息</param>
  97. /// <param name="type">异常类型</param>
  98. public static void ThrowBarCodeException(string message, BarCodeType type)
  99. {
  100. BarCodeException exception = new BarCodeException(message);
  101. exception.BarCodeType = type;
  102. throw exception;
  103. }
  104. }
  105. }