只能写写简单的接口公司用了。太难了这行。。

自定义进制转换器demo
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HC_demo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//十转八
{
if (textBox3_dec.Text.Length != 0)
{
Int32 i;
try
{
i = Convert.ToInt32(textBox3_dec.Text.Trim());
textBox2_oct.Text = Convert.ToString(i, 8);
}
catch
{
MessageBox.Show(“请输入合法的十进制数”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show(“请提供转换数据!”, “提示”, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
textBox3_dec.Focus();
}

    private void button1_Click_1(object sender, EventArgs e)//十转二
    {
        if (textBox3_dec.Text.Length != 0)
        {
            long i;
            try
            {
                i = Convert.ToInt64(textBox3_dec.Text.Trim());
                textBox1_bin.Text = Convert.ToString(i, 2);
            }
            catch
            {
                MessageBox.Show("请输入合法的十进制数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("请提供转换数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        textBox3_dec.Focus();
    }

    private void button2_Click(object sender, EventArgs e)//十转十六
    {
        if (textBox3_dec.Text.Length != 0)
        {
            Int32 i;
            try
            {
                i = Convert.ToInt32(textBox3_dec.Text.Trim());
                textBox4_hex.Text = Convert.ToString(i, 16);
            }
            catch
            {
                MessageBox.Show("请输入合法的十进制数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("请提供转换数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        textBox3_dec.Focus();
    }

    private void button4_Click(object sender, EventArgs e)//十六转十
    {
        if (textBox4_hex.Text.Length != 0)
        {
            try
            {
                string temp = "";
                temp = textBox4_hex.Text.Substring(8, 12);
                textBox3_dec.Text = Convert.ToString(Convert.ToInt64(temp, 16));
            }
            catch
            {
                MessageBox.Show("请提供合法的十六进制数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("请提供转换数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        textBox4_hex.Focus();
    }

    private void button5_Click(object sender, EventArgs e)//十六转十再转二再转十
    {
        /*button4_Click(sender, e);
        button1_Click_1(sender, e);
        button7_Click(sender, e);*/

        return_EPCData(textBox4_hex.Text);
    }

    private void button6_Click(object sender, EventArgs e)//八转十
    {
        if (textBox2_oct.Text.Length != 0)
        {
            try
            {
                textBox3_dec.Text = Convert.ToString(Convert.ToInt32(textBox2_oct.Text.Trim(), 8));//八进制转为十进制
            }
            catch
            {
                MessageBox.Show("请提供合法的八进制数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("请提供转换数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        textBox2_oct.Focus();
    }

    private void button7_Click(object sender, EventArgs e)//二转十
    {
        if (textBox1_bin.Text.Length != 0)
        {
            try
            {
                string temp;
                temp = textBox1_bin.Text.Substring(1, 45);
                textBox3_dec.Text = Convert.ToString(Convert.ToInt64(temp, 2));
                string ret = textBox3_dec.Text.Remove(0,1);
                textBox3_dec.Text = ret;
            }
            catch
            {
                MessageBox.Show("请提供合法的二进制数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("请提供转换数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        textBox1_bin.Focus();
    }

    private void button8_Click(object sender, EventArgs e)//倒叙十六转十
    {
        return_ReaderNo(textBox4_hex.Text);
        /*if (textBox4_hex.Text.Length != 0)
        {
            try
            {
                string temp1 = textBox4_hex.Text.Substring(0, 2);
                string temp2 = textBox4_hex.Text.Substring(2, 2);
                string temp3 = textBox4_hex.Text.Substring(4, 2);
                string temp4 = textBox4_hex.Text.Substring(6, 2);

                string ret = "";

                ret = temp4 + temp3 + temp2 + temp1;
                
                textBox3_dec.Text = Convert.ToString(Convert.ToInt64(ret, 16));
            }
            catch
            {
                MessageBox.Show("请提供合法的十六进制数!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        else
        {
            MessageBox.Show("请提供转换数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        textBox4_hex.Focus();*/
    }

    private string return_EPCData(string hex)
    {
        if (hex.Length != 0)
        {
            //十六进制转十进制
            string temp_hex = "";
            temp_hex = hex.Substring(8, 12);
            string ret_dec = Convert.ToString(Convert.ToInt64(temp_hex, 16));

            //十进制转二进制
            long i;
            i = Convert.ToInt64(ret_dec);
            string temp_bin = "";
            temp_bin = Convert.ToString(i, 2);
            
			temp_bin = temp_bin.ToString().PadLeft(48, '0');//二进制不足48位的补足
			
            //二进制转十进制
            string temp_sub;
            //temp_sub = temp_bin.Substring(1, 45);
            temp_sub = temp_bin.Substring(temp_bin.Length - 45, 45);//取二进制后面的45位
            string temp_dec2 = Convert.ToString(Convert.ToInt64(temp_sub, 2));
            string ret_dec2 = temp_dec2.Remove(0, 1);
            string return_dec = ret_dec2;

            textBox3_dec.Text = return_dec;
            return return_dec;
        }
        else
        {
            return "-1";
        }
    }

    private string return_ReaderNo(string readerNo)//倒叙十六进制转二进制
    {
        if (readerNo.Length != 0)
        {
            string temp1 = readerNo.Substring(0, 2);
            string temp2 = readerNo.Substring(2, 2);
            string temp3 = readerNo.Substring(4, 2);
            string temp4 = readerNo.Substring(6, 2);

            string ret = "";

            ret = temp4 + temp3 + temp2 + temp1;

            string returnReaderNo;

            returnReaderNo = Convert.ToString(Convert.ToInt64(ret, 16));

            textBox3_dec.Text = returnReaderNo;

            return returnReaderNo;
        }
        else
        {
            return "-1";
        }
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值