C#實(shí)現(xiàn)一個(gè)相當(dāng)全面的數(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)文章
C#使用時(shí)序數(shù)據(jù)庫InfluxDB的教程詳解
InfluxDB是一個(gè)開源的時(shí)序數(shù)據(jù)庫,可以自動(dòng)處理時(shí)間序列數(shù)據(jù),這篇文章主要為大家詳細(xì)介紹了C#如何使用InfluxDB,感興趣的小伙伴可以跟隨小編一起了解下2023-11-11
C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例
下面小編就為大家分享一篇C#使用Gembox.SpreadSheet向Excel寫入數(shù)據(jù)及圖表的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
C#編程中使用設(shè)計(jì)模式中的原型模式的實(shí)例講解
這篇文章主要介紹了C#編程中使用設(shè)計(jì)模式中的原型模式的實(shí)例講解,原型模式創(chuàng)建新對(duì)象方便快捷,而且可在運(yùn)行時(shí)根據(jù)需要通過克隆來添加和去除他們,也可在程序運(yùn)行是根據(jù)情況來修改類內(nèi)部的數(shù)據(jù),需要的朋友可以參考下2016-02-02
在winform下實(shí)現(xiàn)左右布局多窗口界面的方法之續(xù)篇
這篇文章主要介紹了在winform下實(shí)現(xiàn)左右布局多窗口界面的方法之續(xù)篇 的相關(guān)資料,需要的朋友可以參考下2016-02-02

