⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvarstring.cs

📁 短信网关的应用开发
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace SMPP.Variable
{
	/// <summary>
	/// 变长字符串类型
	/// 存储以NULL结尾的字符串
	/// </summary>
	public class CVARString : Variable
	{
		/// <summary>
		/// 保存的真实数据
		/// </summary>
		protected byte[] _Value = new byte[1];

		private int _MaxLength = 1;
		private int MaxLength
		{
			get
			{
				return _MaxLength;
			}
			set
			{
				_MaxLength = value < 1 ? 1 : value;
			}
		}

		public override int Length
		{
			get
			{
				return _Value.Length;
			}
		}

		private System.Text.Encoding Encoding = System.Text.Encoding.ASCII;

		public string Value
		{
			get
			{
				if (_Value.Length == 0)
					return "";
				else
				{
					return Encoding.GetString(_Value, 0, Array.IndexOf(_Value, (byte)0));
				}
			}
			set
			{
				if (value == null || value == "")
				{//如果值为空或者空字符串
					_Value = new byte[1];
					_Value[0] = 0;
				}
				else
				{
					byte[] data = Encoding.GetBytes(value);

					_Value = new byte[data.Length + 1];

					Array.Clear(_Value, 0, _Value.Length);
					Array.Copy(data, _Value, data.Length);
				}
			}
		}

		public override void Parse(byte[] Datas, ref int Index)
		{
			int index = Array.IndexOf(Datas, (byte)0, Index);

			if (index == -1)
				throw new Exception("没有找到以NULL结尾的串");

			if (index - Index == 0)
			{
				Value = "";
				Index++;
			}
			else
			{
				_Value = new byte[index - Index + 1];
				Array.Copy(Datas, Index, _Value, 0, index - Index);

				Index = index + 1;
			}
		}

		public override byte[] GetBytes()
		{
			byte[] ret = new byte[_Value.Length];

			Array.Copy(_Value, ret, ret.Length);

			return ret;
		}

		public CVARString(int MaxLength)
		{
			this.MaxLength = MaxLength;
		}

		public CVARString(int MaxLength, System.Text.Encoding Encoding)
		{
			this.MaxLength = MaxLength;
			this.Encoding = Encoding;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -