TryParse(String, Int32)方法

目录

一、定义:

二、示例:

三、生成:


一、定义:

        将数字的字符串表示形式转换为它的等效 32 位有符号整数。 一个指示转换是否成功的返回值。

public static bool TryParse (string? s, out int result);

参数s String 
包含要转换的数字的字符串。

result Int32
当此方法返回时,如果转换成功,则包含与 s 中所包含的数字等效的 32 位无符号整数值;如果转换失败,则包含零。 如果参数为 null 或 Empty,格式不正确,或表示小于 Int32.MinValue 或大于 Int32.MaxValue 的数字,则转换失败。s 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。

返回Boolean
如果 true 成功转换,则为 s;否则为 false。

二、示例:

// TryParse(String, Int32)
// 用异或运算法对数字加密解密
namespace _018
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private GroupBox? groupBox2;
        private Button? button1;
        private TextBox? textBox3;
        private TextBox? textBox2;
        private TextBox? textBox1;
        private Label? label3;
        private Label? label2;
        private Label? label1;
        private TextBox? textBox4;
        private Label? label4;
        private Button? button2;

        public Form1()
        {
            InitializeComponent();
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {          
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(125, 106),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 6,
                Text = "开始加密",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // textBox3
            // 
            textBox3 = new TextBox
            {
                Location = new Point(100, 77),
                Name = "textBox3",
                Size = new Size(100, 23),
                TabIndex = 5
            };
            // 
            // textBox2
            // 
            textBox2 = new TextBox
            {
                Location = new Point(100, 48),
                Name = "textBox2",
                Size = new Size(100, 23),
                TabIndex = 4
            };
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(100, 19),
                Name = "textBox1",
                Size = new Size(100, 23),
                TabIndex = 3
            };
            // 
            // label3
            // 
            label3 = new Label
            {
                AutoSize = true,
                Location = new Point(16, 83),
                Name = "label3",
                Size = new Size(43, 17),
                TabIndex = 2,
                Text = "加密后数字:"
            };
            // 
            // label2
            // 
            label2 = new Label
            {
                AutoSize = true,
                Location = new Point(16, 54),
                Name = "label2",
                Size = new Size(43, 17),
                TabIndex = 1,
                Text = "输入加密数字:"
            };
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(16, 25),
                Name = "label1",
                Size = new Size(43, 17),
                TabIndex = 0,
                Text = "输入数字:"
            };         
            // 
            // textBox4
            // 
            textBox4 = new TextBox
            {
                Location = new Point(99, 22),
                Name = "textBox4",
                Size = new Size(100, 23),
                TabIndex = 9
            };
            // 
            // label4
            // 
            label4 = new Label
            {
                AutoSize = true,
                Location = new Point(16, 28),
                Name = "label4",
                Size = new Size(43, 17),
                TabIndex = 8,
                Text = "解密后数字:"
            };
            // 
            // button2
            // 
            button2 = new Button
            {
                Location = new Point(124, 54),
                Name = "button2",
                Size = new Size(75, 23),
                TabIndex = 7,
                Text = "开始解密",
                UseVisualStyleBackColor = true
            };
            button2.Click += Button2_Click;
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(12, 12),
                Name = "groupBox1",
                Size = new Size(205, 140),
                TabIndex = 0,
                TabStop = false,
                Text = "加密:"
            };
            groupBox1.Controls.Add(button1);
            groupBox1.Controls.Add(textBox3);
            groupBox1.Controls.Add(textBox2);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(label3);
            groupBox1.Controls.Add(label2);
            groupBox1.Controls.Add(label1);
            groupBox1.SuspendLayout();
            // 
            // groupBox2
            // 
            groupBox2 = new GroupBox
            {
                Location = new Point(12, 156),
                Name = "groupBox2",
                Size = new Size(205, 80),
                TabIndex = 0,
                TabStop = false,
                Text = "解密:"
            };
            groupBox2.Controls.Add(textBox4);
            groupBox2.Controls.Add(label4);
            groupBox2.Controls.Add(button2);
            groupBox2.SuspendLayout();
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(224, 251);
            Controls.Add(groupBox2);
            Controls.Add(groupBox1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "Form1";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
            groupBox2.ResumeLayout(false);
            groupBox2.PerformLayout();
            ResumeLayout(false);
        }
        /// <summary>
        /// 加密
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            if (int.TryParse(textBox1!.Text, out int P_int_Num)                  //内联变量并判断输入是否是数值
               && int.TryParse(textBox2!.Text, out int P_int_Key))
            {
                textBox3!.Text = (P_int_Num ^ P_int_Key).ToString();     //加密数值
            }
            else
            {
                MessageBox.Show("请输入数值", "出现错误!");              //提示输入信息不正确
            }
        }
        /// <summary>
        /// 解密
        /// </summary>
        private void Button2_Click(object? sender, EventArgs e)
        {
            if (int.TryParse(textBox3!.Text, out int P_int_Key)               //内联变量并判断输入是否是数值
                && int.TryParse(textBox2!.Text, out int P_int_Encrypt))
            {
                textBox4!.Text = (P_int_Encrypt ^ P_int_Key).ToString();   //解密数值
            }
            else
            {
                MessageBox.Show("请输入数值", "出现错误!");              //提示输入信息不正确
            }
        }
    }
}

三、生成:

 

相关推荐

  1. STM32 SDIO接口配置与使用方法详解

    2023-12-31 10:38:02       64 阅读
  2. 【心得】关于STM32中RTC的校准方法

    2023-12-31 10:38:02       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-31 10:38:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-31 10:38:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-31 10:38:02       82 阅读
  4. Python语言-面向对象

    2023-12-31 10:38:02       91 阅读

热门阅读

  1. Chocolatey

    2023-12-31 10:38:02       58 阅读
  2. centos7 磁盘逻辑卷扩容

    2023-12-31 10:38:02       50 阅读
  3. 【C++】循环结构中的变量的生命周期

    2023-12-31 10:38:02       59 阅读
  4. node: /lib64/libm.so.6: version `GLIBC_2.27‘ not found

    2023-12-31 10:38:02       56 阅读
  5. 多态的底层实现原理和泛型的底层实现原理

    2023-12-31 10:38:02       57 阅读
  6. C++ 具名要求

    2023-12-31 10:38:02       44 阅读
  7. C++ 类打包LIB方法,创建 C 接口函数方法

    2023-12-31 10:38:02       59 阅读
  8. 通信原理课设(gec6818) 006:网络编程

    2023-12-31 10:38:02       46 阅读
  9. Python使用PyMySql增删改查Mysql数据库

    2023-12-31 10:38:02       65 阅读