char
1 char[] digits = "0123456789abcdef".ToCharArray();
int
(1)使用強制型別轉換:(int)浮點數
(2)使用Convert.ToInt32(string)
(3)使用 int.Parse(string) 或 int.TryParse(string,out int)
在實際使用時,當要轉換的字串或數字帶有小數時,發現它們有以下區別:
(1)方法一:截斷 方法二:四捨五入 int a=(int)2.8; //結果為2 int b=Convert.ToInt32(2.8); //b的值為3
(2)int.Parse方法的引數如果不能轉換為整數,則報異常。 如 int c=int.Parse("2.8"); //報異常,說明其引數必須是整數字符串
1 //int.TryParse 2 3 int c = -1; 4 5 int.TryParse("2.8", out c); //不能轉換成功,結果為0 6 7 int.TryParse("2", out c); //轉換成功,結果為2
string to int
1 int a = (int)'a'; //結果為97,注意是字元,而不是字串(如果是字串,編譯不能通過) 2 3 int b = Convert.ToInt32("a"); //報異常 4 5 int c=int.Parse("a"); //報異常 6 7 int d = -1; 8 9 int.TryParse("a", out d); //結果為0
可變字串
1 string[] HexPadding = new string[16]; 2 3 int padding = HexPadding.Length - i; 4 5 var buf = new StringBuilder(padding * 3); 6 7 buf.Append(" ");
byte[] + byte[]
1 /// <summary> 2 /// 拼接二進位制陣列 3 /// </summary> 4 /// <param name="buffer1"></param> 5 /// <param name="buffer2"></param> 6 /// <returns></returns> 7 public static byte[] Joint(byte[] buffer1, byte[] buffer2) 8 { 9 byte[] bufferWhole = new byte[buffer1.Length + buffer2.Length]; 10 Buffer.BlockCopy(buffer1, 0, bufferWhole, 0, buffer1.Length); 11 Buffer.BlockCopy(buffer2, 0, bufferWhole, buffer1.Length, buffer2.Length); 12 return bufferWhole; 13 } 14 15 public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3) 16 { 17 return BufferJointer.Joint(BufferJointer.Joint(buffer1, buffer2), buffer3); 18 } 19 20 public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4) 21 { 22 return BufferJointer.Joint(BufferJointer.Joint(buffer1, buffer2, buffer3), buffer4); 23 } 24 25 public static byte[] Joint(byte[] buffer1, byte[] buffer2, byte[] buffer3, byte[] buffer4, byte[] buffer5, byte[] buffer6) 26 { 27 return BufferJointer.Joint(BufferJointer.Joint(buffer1, buffer2, buffer3, buffer4), buffer5, buffer6); 28 }
string to byte[] to uft-8
1 public static string get_uft8(string unicodeString) 2 { 3 UTF8Encoding utf8 = new UTF8Encoding(); 4 Byte[] encodedBytes = utf8.GetBytes(unicodeString); 5 String decodedString = utf8.GetString(encodedBytes); 6 return decodedString; 7 }
1 class FileHelper 2 { 3 // 首先引用API 函式 4 5 [DllImport("kernel32.dll")] 6 private static extern IntPtr _lopen(string lpPathName, int iReadWrite); 7 [DllImport("kernel32.dll")] 8 private static extern bool CloseHandle(IntPtr hObject); 9 private const int OF_READWRITE = 2; 10 private const int OF_SHARE_DENY_NONE = 0x40; 11 private readonly IntPtr HFILE_ERROR = new IntPtr(-1); 12 13 14 /// <summary> 15 /// 檢查檔案是否已經開啟 16 /// </summary> 17 /// <param name="strfilepath">要檢查的檔案路徑</param> 18 /// <returns>-1檔案不存在,1檔案已經開啟,0檔案可用未開啟</returns> 19 public FileStatus VerifyFileIsOpen(string strfilepath) 20 { 21 string vFileName = strfilepath; 22 23 // 先檢查檔案是否存在,如果不存在那就不檢查了 24 if (!File.Exists(vFileName)) 25 { 26 return FileStatus.Inexistence; 27 } 28 29 // 開啟指定檔案看看情況 30 IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE); 31 if (vHandle == HFILE_ERROR) 32 { // 檔案已經被開啟 33 return FileStatus.Opened; 34 } 35 CloseHandle(vHandle); 36 37 // 說明檔案沒被開啟,並且可用 38 39 return FileStatus.Untapped; 40 } 41 }