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)文章
Unity?百度AI實(shí)現(xiàn)Logo商標(biāo)識別
本文主要介紹了Unity實(shí)現(xiàn)檢測和識別圖片中的品牌LOGO信息。即對于輸入的一張圖片(可正常解碼,且長寬比適宜),輸出圖片中LOGO的名稱、位置和置信度。需要的可以參考一下2022-01-01C#中靜態(tài)構(gòu)造函數(shù)的幾點(diǎn)說明介紹
本篇文章主要是對C#中靜態(tài)構(gòu)造函數(shù)的幾點(diǎn)說明進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01Unity3D 計(jì)時(shí)器的實(shí)現(xiàn)代碼(三種寫法總結(jié))
這篇文章主要介紹了Unity3D 計(jì)時(shí)器的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#?PaddleOCRSharp?OCR進(jìn)行疲勞測試
PaddleOCRSharp?是百度飛槳封裝的.NET版本?OCR?dll?類庫,OCR可以將圖像文件中的文本內(nèi)容進(jìn)行識別,下面我們就來看看如何通過它們實(shí)現(xiàn)疲勞測試吧2024-11-11C# WinForm控件對透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡單解決方法
這篇文章主要介紹了C# WinForm控件對透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡單解決方法,結(jié)合實(shí)例形式分析了WinForm圖片重疊后造成圖片不透明的原因與相應(yīng)的解決方法,需要的朋友可以參考下2016-06-06