亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#實(shí)現(xiàn)一個(gè)相當(dāng)全面的數(shù)據(jù)轉(zhuǎn)換工具類

 更新時(shí)間:2025年03月06日 10:29:17   作者:WangMing_X  
這篇文章主要為大家介紹了如何使用C#編寫一個(gè)通用工具類DataConvert來進(jìn)行數(shù)據(jù)轉(zhuǎn)換,包括30+個(gè)數(shù)據(jù)類型轉(zhuǎn)換,需要的可以了解一下

C#通用工具類DataConvert,作為靜態(tài)類全局可調(diào)用,來進(jìn)行數(shù)據(jù)轉(zhuǎn)換。包括byte[]轉(zhuǎn)數(shù)字、CSV、數(shù)字轉(zhuǎn)byte[]、16進(jìn)制數(shù)轉(zhuǎn)換、TryParse、DateTime等。

一、具體函數(shù)列表

default部分函數(shù)

//default
 
public static string ArrayToString<T>(T[] array)
public static string ListToString<T>(List<T> list)

byte[]轉(zhuǎn)數(shù)字部分函數(shù)

//byte[]轉(zhuǎn)數(shù)字
 
/// <summary>
/// byte數(shù)組中取int數(shù)值,本方法適用于(低位在前,高位在后)的順序
/// </summary>
/// <param name="src">byte數(shù)組 </param>
/// <param name="offset"> 從數(shù)組的第offset位開始 </param>
/// <returns>int數(shù)值</returns>
public static int BytesToInt32(byte[] src, int offset)
 
/// <summary>
///  byte數(shù)組中取int數(shù)值,本方法適用于(低位在后,高位在前)的順序
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static int BytesToInt32R(byte[] src, int offset)
 
/// <summary>
/// byte數(shù)組中取short數(shù)值,本方法適用于(低位在前,高位在后)的順序
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static short BytesToInt16(byte[] src, int offset)
 
/// <summary>
/// byte數(shù)組中取short數(shù)值,本方法適用于(低位在后,高位在前)的順序
/// </summary>
/// <param name="src"></param>
/// <param name="offset"></param>
/// <returns></returns>
public static short BytesToInt16R(byte[] src, int offset)

數(shù)字轉(zhuǎn)byte[]部分函數(shù)

/// <summary>
/// Int16轉(zhuǎn)換成byte[] (低位在前,高位在后)的順序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int16ToBytes(short num)
 
/// <summary>
/// Int16轉(zhuǎn)換成byte[] (低位在后,高位在前)的順序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int16ToBytesR(short num)
 
/// <summary>
/// Int32轉(zhuǎn)換成byte[] (低位在前,高位在后)的順序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
 public static byte[] Int32ToBytes(int num)
 
/// <summary>
/// Int32轉(zhuǎn)換成byte[] (低位在后,高位在前)的順序
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
public static byte[] Int32ToBytesR(int num)
 

16進(jìn)制數(shù)轉(zhuǎn)換部分函數(shù)

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部分函數(shù)

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部分函數(shù)

 private static readonly DateTime
 /// <summary>
 /// DateTime轉(zhuǎn)10位時(shí)間戳
 /// </summary>
 /// <param name="dateTime"></par
 /// <returns></returns>
public static long DateTimeToTimeStamp10(DateTime dateTime)
 
/// <summary>
/// DateTime轉(zhuǎn)13位時(shí)間戳
/// </summary>
/// <param name="dateTime"></param>
/// <returns></returns>
public static long DateTimeToTimeStamp13(DateTime dateTime)
 
/// <summary>
/// 10位時(shí)間戳轉(zhuǎn)DateTime
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime TimeStamp10ToDateTime(long timeStamp)
 
/// <summary>
/// 13位時(shí)間戳轉(zhuǎn)DateTime
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
public static DateTime TimeStamp13ToDateTime(long timeStamp)
 

二、函數(shù)調(diào)用示例

1、引用DataConvert類的命名空間(或修改類文件的命名空間為當(dāng)前項(xiàng)目)

2、在引用了DataConvert命名空間的項(xiàng)目里面直接用類名點(diǎn)出內(nèi)部的轉(zhuǎn)換函數(shù)

int rssi= DataConvert.HexToDecimal(“C3”);

以上就是C#實(shí)現(xiàn)一個(gè)相當(dāng)全面的數(shù)據(jù)轉(zhuǎn)換工具類的詳細(xì)內(nèi)容,更多關(guān)于C#數(shù)據(jù)轉(zhuǎn)換的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論