字节和数字,字符串转换
来源:互联网 发布:js给json对象添加属性 编辑:程序博客网 时间:2024/05/16 05:01
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, 0, 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[] GetDateTimeBytes(DateTime datetime)
{
var bytes = new byte[6];
bytes[0] = byte.Parse(datetime.Year.ToString("0000").Substring(2, 2));
bytes[1] = (byte)datetime.Month;
bytes[2] = (byte)datetime.Day;
bytes[3] = (byte)datetime.Hour;
bytes[4] = (byte)datetime.Minute;
bytes[5] = (byte)datetime.Second;
return bytes;
}
/// <summary>
/// 获取时间字节序
/// </summary>
public static byte[] GetDateTimeBytes(string datetime)
{
var bytes = new byte[6];
if (!string.IsNullOrWhiteSpace(datetime))
{
bytes[0] = byte.Parse(datetime.Substring(0, 2));
bytes[1] = byte.Parse(datetime.Substring(2, 2));
bytes[2] = byte.Parse(datetime.Substring(4, 2));
bytes[3] = byte.Parse(datetime.Substring(6, 2));
bytes[4] = byte.Parse(datetime.Substring(8, 2));
bytes[5] = byte.Parse(datetime.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;
}
}
- 字节和数字,字符串转换
- 字符串和数字转换
- php 数字和字符串转换
- 字符串和数字相互转换
- 数字和字符串的转换
- 字符串和数字相互转换
- 字符串和数字的转换
- 字符串和数字相互转换
- 数字和字符串的转换
- python3 字符串和字节之间的转换
- 字符串和字节数组之间的转换
- 字符串和字节的相互转换
- 字节数组和字符串相互转换
- python3中字符串和字节之间转换
- Python字符串和字节类型转换
- 宽字节和单字节字符串相互转换
- 字符串转换为字符串数组和字节数组,字符串数组转换为字节数组
- VC多字节字符串和宽字节字符串转换
- SQL Server IDENTITY用法
- c++ 对关键段CRITICAL_SECTION的封装
- 浅析 Java Thread.join()
- Sql Server identity VS uniqueidentifier
- Ubuntu配置嵌入式交叉编译环境
- 字节和数字,字符串转换
- 关于handler的部分注意点
- android分辨率单位等比值
- POI3.8读取word文档的表格数据!
- linux环境下android常用工具和技巧
- 使用套接字实现ICMP协议
- 【Android】 在Andorid中解析Json数据示例
- Java高新技术学习日记
- 多表关联的update语句