C#通用工具类DataConvert,作为静态类全局可调用,来进行数据转换。包括byte[]转数字、CSV、数字转byte[]、16进制数转换、TryParse、DateTime等。
源cs类文件下载链接:夸克网盘分享
一、具体函数列表
default部分函数
//default
public static string ArrayToString<T>(T[] array)
public static string ListToString<T>(List<T> list)
byte[]转数字部分函数
//byte[]转数字
/// <summary>
/// byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序
/// </summary>
/// <param name="src">byte数组 </param>
/// <param name="offset"> 从数组的第offset位开始 </param>
/// <returns>int数值</returns>
public static int BytesToInt32(byte[] src, int offset)
/// <summary>
/// byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static int BytesToInt32R(byte[] src, int offset)
/// <summary>
/// byte数组中取short数值,本方法适用于(低位在前,高位在后)的顺序
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static short BytesToInt16(byte[] src, int offset)
/// <summary>
/// byte数组中取short数值,本方法适用于(低位在后,高位在前)的顺序
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static short BytesToInt16R(byte[] src, int offset)
数字转byte[]部分函数
/// <summary>
/// Int16转换成byte[] (低位在前,高位在后)的顺序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int16ToBytes(short num)
/// <summary>
/// Int16转换成byte[] (低位在后,高位在前)的顺序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int16ToBytesR(short num)
/// <summary>
/// Int32转换成byte[] (低位在前,高位在后)的顺序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int32ToBytes(int num)
/// <summary>
/// Int32转换成byte[] (低位在后,高位在前)的顺序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int32ToBytesR(int num)
16进制数转换部分函数
public static string byteArrayToHexString(byte[] data)
public static byte[] StrToHexBytes(string hexString)
/// <summary>
/// "03E8"→1000
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public static int HexToDecimal(string hex)
TryParse部分函数
public static bool BoolTryParse(string str, ref bool value)
public static bool ByteTryParse(string str, ref byte value)
public static bool ShortTryParse(string str, ref short value)
public static bool UshortTryParse(string str, ref ushort value)
public static bool IntTryParse(string str, ref int value)
public static bool FloatTryParse(string str, ref float value)
public static bool DoubleTryParse(string str, ref double value)
public static bool EnumTryParse<T>(string str, ref T value)
DateTime部分函数
private static readonly DateTime
/// <summary>
/// DateTime转10位时间戳
/// </summary>
/// <param name="dateTime"></par
/// <returns></returns>
public static long DateTimeToTimeStamp10(DateTime dateTime)
/// <summary>
/// DateTime转13位时间戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static long DateTimeToTimeStamp13(DateTime dateTime)
/// <summary>
/// 10位时间戳转DateTime
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime TimeStamp10ToDateTime(long timeStamp)
/// <summary>
/// 13位时间戳转DateTime
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime TimeStamp13ToDateTime(long timeStamp)
二、函数调用示例
1、引用DataConvert类的命名空间(或修改类文件的命名空间为当前项目)
编辑
编辑
2、在引用了DataConvert命名空间的项目里面直接用类名点出内部的转换函数
int rssi= DataConvert.HexToDecimal(“C3”);