博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符类型转换
阅读量:4465 次
发布时间:2019-06-08

本文共 3689 字,大约阅读时间需要 12 分钟。

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 }

 

转载于:https://www.cnblogs.com/endv/p/6006294.html

你可能感兴趣的文章
《C程序设计语言》笔记 (八) UNIX系统接口
查看>>
git常用命令
查看>>
Android必知必会-获取视频文件的截图、缩略图
查看>>
(转)理解Bitblt、StretchBlt与SetDIBitsToDevice、StretchDibits
查看>>
python之路-基础篇-第七周
查看>>
高性能队列Disruptor系列2--浅析Disruptor
查看>>
ViurtualBox配置虚拟机Linux的网络环境
查看>>
VLC 媒体播放器
查看>>
勿忘国耻2018/09/18
查看>>
Jenkins部署码云SpringBoot项目
查看>>
多标签分类(multi-label classification)综述
查看>>
史上最全面的Spring-Boot-Cache使用与整合
查看>>
图的遍历(深度优先与广度优先搜索两种方案)
查看>>
快速读入模板
查看>>
把一张图片变成base64
查看>>
impdp报错: ORA-39064: 无法写入日志文件 ORA-29285: 文件写入错误
查看>>
\n ^ \t的使用
查看>>
css盒模型
查看>>
计算机学科各专业大牛
查看>>
MongoDB配置--同一台服务器搭建多MongoDB实例
查看>>