ScanCodeManager.cs 1.8 KB
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
 

namespace OnlineStore.Common
{
    /// <summary>
    /// 扫码枪管理类
    /// </summary>
    public class ScanCodeManager
    {
     //   public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// 处理接收后的二维码
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string ReplaceCode(string message)
        {
            message = message.Trim();
            message = message.Replace("\r", "");
            message = message.Replace("\n", "");
            char a = (char)02;
            message = message.Replace(a.ToString(), "");
            message = message.Trim();
            System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
            byte[] bytes = asciiEncoding.GetBytes(message);
            List<byte> newBytes =new List<byte> ();
            foreach (byte by in bytes)
            {
                if (!by.Equals(24))
                {
                    newBytes.Add(by);
                }
            }
            message = asciiEncoding.GetString(newBytes.ToArray());
            //if (message.Length == 1)
            //{
            //    try
            //    {
            //        int intAsciiCode = (int)asciiEncoding.GetBytes(message)[0];
            //        if (intAsciiCode.Equals(24))
            //        {
            //            return "";
            //        }
            //    }
            //    catch (Exception ex)
            //    {
            //        LOGGER.Error(ex);
            //    }
            //} 
            return message;
        }

    }
}