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.TryParse2 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 ///2 /// 拼接二进制数组 3 /// 4 /// 5 /// 6 ///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 ///15 /// 检查文件是否已经打开 16 /// 17 /// 要检查的文件路径 18 ///-1文件不存在,1文件已经打开,0文件可用未打开 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 }