大佬教程收集整理的这篇文章主要介绍了字节公共操作类,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
/// <sumMary>
/// 字节公共操作类
/// </sumMary>
public static class BytesUtility
{
public static String ToHexString(this byte[] data)
{
if (data == null)
return String.Empty;
StringBuilder builder = new StringBuilder(100);
foreach (var item in data)
{
builder.AppendFormat("{0:X2} ",item);
}
return builder.ToString();
}
/// <sumMary>
/// 获取字符串
/// </sumMary>
public static String GetString(byte[] data)
{
#if SILVERLIGHT
return System.Text.Encoding.Unicode.GetString(data,data.Length);
#else
return System.Text.Encoding.Unicode.GetString(data);
#endif
}
/// <sumMary>
/// 获取GBK编码字符串
/// </sumMary>
public static String GetGBKString(byte[] data)
{
#if SILVERLIGHT
return GBKEncoder.Read(data);
#else
return System.Text.Encoding.GetEncoding("gbk").GetString(data);
#endif
}
/// <sumMary>
/// 获取字节序
/// </sumMary>
public static byte[] GetBytes(String value)
{
return System.Text.Encoding.Unicode.GetBytes(value);
}
/// <sumMary>
/// 获取GBK字节序
/// </sumMary>
public static byte[] GetGBKBytes(String value)
{
#if SILVERLIGHT
return GBKEncoder.ToBytes(value);
#else
return System.Text.Encoding.GetEncoding("gbk").GetBytes(value);
#endif
}
/// <sumMary>
/// 获取时间字节序
/// </sumMary>
public static byte[] GetdatetiR_183_11845@eBytes(datetiR_183_11845@e datetiR_183_11845@E)
{
var bytes = new byte[6];
bytes[0] = byte.Parse(datetiR_183_11845@e.Year.ToString("0000").SubString(2,2));
bytes[1] = (bytE)datetiR_183_11845@e.Month;
bytes[2] = (bytE)datetiR_183_11845@e.Day;
bytes[3] = (bytE)datetiR_183_11845@e.Hour;
bytes[4] = (bytE)datetiR_183_11845@e.minute;
bytes[5] = (bytE)datetiR_183_11845@e.Second;
return bytes;
}
/// <sumMary>
/// 获取时间字节序
/// </sumMary>
public static byte[] GetdatetiR_183_11845@eBytes(String datetiR_183_11845@E)
{
var bytes = new byte[6];
if (!String.IsNullOrWhiteSpace(datetiR_183_11845@E))
{
bytes[0] = byte.Parse(datetiR_183_11845@e.SubString(0,2));
bytes[1] = byte.Parse(datetiR_183_11845@e.SubString(2,2));
bytes[2] = byte.Parse(datetiR_183_11845@e.SubString(4,2));
bytes[3] = byte.Parse(datetiR_183_11845@e.SubString(6,2));
bytes[4] = byte.Parse(datetiR_183_11845@e.SubString(8,2));
bytes[5] = byte.Parse(datetiR_183_11845@e.SubString(10,2));
}
return bytes;
}
/// <sumMary>
/// 获取字节序
/// </sumMary>
public static byte[] GetBytes(int value)
{
byte[] bytes = new byte[4];
bytes[0] = (bytE)(value >> 0x18);
bytes[1] = (bytE)(value >> 0x10);
bytes[2] = (bytE)(value >> 8);
bytes[3] = (bytE)(value);
return bytes;
}
/// <sumMary>
/// 获取字节序
/// </sumMary>
public static byte[] GetBytes(ushort value)
{
byte[] bytes = new byte[2];
bytes[0] = (bytE)(value >> 8);
bytes[1] = (bytE)(value);
return bytes;
}
/// <sumMary>
/// 获取字节序
/// </sumMary>
public static byte[] GetBytes(UInt32 value)
{
byte[] bytes = new byte[4];
bytes[0] = (bytE)(value >> 0x18);
bytes[1] = (bytE)(value >> 0x10);
bytes[2] = (bytE)(value >> 8);
bytes[3] = (bytE)(value);
return bytes;
}
/// <sumMary>
/// 获取某位是否为1
/// </sumMary>
public static bool GetBitStatus(uint value,int indeX)
{
return ((value >> indeX) & 0x01) == 0x01;
}
/// <sumMary>
/// 获取校验码
/// </sumMary>
public static byte XOR(byte[] raw,int lenght)
{
byte A = 0;
for (int i = 0; i < lenght; i++)
{
A ^= raw[i];
}
return A;
}
/// <sumMary>
/// 获取校验码
/// </sumMary>
public static byte XOR(byte[] raw,int index,int lenght)
{
byte A = 0;
for (int i = index; i < index + lenght; i++)
{
A ^= raw[i];
}
return A;
}
/// <sumMary>
/// 获取某位是否为1
/// </sumMary>
public static bool GetBitStatus(int value,int indeX)
{
return ((value >> indeX) & 0x01) == 0x01;
}
/// <sumMary>
/// 设置某位为0或者1
/// </sumMary>
public static uint SetBitStatus(uint value,bool bit)
{
uint x = ((uint)1) << index;
if (bit)
return value | x;
else
{
x = ~x;
return value & x;
}
}
}
以上是大佬教程为你收集整理的字节公共操作类全部内容,希望文章能够帮你解决字节公共操作类所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。