【问题标题】:Swap Endianess/Byte Order交换字节顺序/字节顺序
【发布时间】:2013-09-08 05:38:54
【问题描述】:

首先我要说我对这个地方和编程很陌生,而且,我只编程了两三天! 好吧,我正在开发一个程序,它基本上从用户那里获取信息并打印出用户需要的 28 字节十六进制代码。解释起来很复杂:P 无论如何,我遇到了一个问题,因为我是新手,不知道如何解决。看,我基本上想告诉我的程序“嘿,将 textBox1 中的值转换为十六进制等效值,但是,如果十六进制等效值是 3 字节值,则在它前面添加一个“0”并交换更改字节序”我已经弄清楚了所有这些,转换为十六进制,3字节的东西,但是,我什至不知道从哪里开始的字节序部分。我试着搜索,但我真的不是很明白,所以请尽量详细解释。

我转换成字符串的十六进制值,以便在文本框中显示。

哦,你知道吗,我之前不打算发布这个,因为它太乱了,但这是我的代码,它只会帮助你帮助我:P

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    int chInt;
    string a1;
    string a2;
    string a3;
    string a4;
    string numAttire;
    string chHex;
    Form2 helpCH = new Form2();
    int check;
    public Form1()
    {
        InitializeComponent();
    }

    private void buttonCHNumberHelp_Click(object sender, EventArgs e)
    {
        helpCH.Show();

    }

    private void textBoxCHNumber_TextChanged(object sender, EventArgs e)
    {
        if (textBoxCHNumber.Text == "00" || textBoxCHNumber.Text == "")
        {
            textBoxAttireNumber.Enabled = false;
        }
        else
        {
            textBoxAttireNumber.Enabled = false;
        }

        if (textBoxCHNumber.Text.Length >= 3)
        {
            textBoxAttireNumber.Enabled = true;
        }

    }

    private void button1_Click(object sender, EventArgs e)
    {
        chInt = Convert.ToInt32(textBoxCHNumber.Text);
        chHex = chInt.ToString("X");
        numAttire = textBoxAttireNumber.Text; //The numAttire is assigned the value currently in textBoxAttireNumber.Text;
        a1 = "09"; //The variable a1 will always be "09" as that is best for the first attire.;
        a2 = textBoxA2.Text; // The contents of textBoxA2 are equal to this variable;
        a3 = textBoxA3.Text; // The contents of textBoxA3 are equal to this variable;
        a4 = textBoxA4.Text; // The contents of textBoxA4 are equal to this variable;

        check = Convert.ToInt32(textBoxAttireNumber.Text);
        bool condition = check > 4; //A local boolean is set up, it's named condition and checks if the variable 'check' is bigger than 4;

        if (condition) //If it is, the Output text box will display "You can only have upto 4 attires";
        {
            textBoxOutput.Text = "You can only have upto 4 attires";
        }

        else //If the statement happens to be false, ie, check IS UNDER 4, the Output text box will display the values:;
        {
            textBoxOutput.Text = chHex + " 00 00 00 0" +numAttire + " 00 00 00 " +a1+ " 00 00 00 FF FF FF FF " + a2 + " 00 00 00 FF FF FF FF " + a3 + " 00 00 00 FF FF FF FF " + a4 + " 00 00 00 FF FF FF FF";

            int chlimit = Convert.ToInt32(textBoxCHNumber.Text);
            bool chlimitbool = chlimit < 100 || chlimit > 343;
            if (chlimitbool) 
            {
                textBoxOutput.Text = "There are no ch slot numbers for values lower than 100 or higher than 343";
            }

            if (textBoxA1.Text == textBoxA2.Text || textBoxA1.Text == textBoxA3.Text || textBoxA1.Text == textBoxA4.Text || textBoxA2.Text == textBoxA3.Text || textBoxA2.Text == textBoxA4.Text || textBoxA3.Text == textBoxA4.Text)  //If the contents of textBoxA1 are the same as the contents of textBoxA2, A3, or A4 then print out "Two attire ID slots may not share the same ID";
            {
                textBoxOutput.Text = "Two attire ID slots may not share the same ID";
            }

            if (textBoxA2.Text.Length >= 4) 
            {
                textBoxOutput.Text = chHex + " 00 00 00 0" + numAttire + " 00 00 00 " + a1 + " 00 00 00 FF FF FF FF " + a2 + " 00 00 FF FF FF FF " + a3 + " 00 00 00 FF FF FF FF " + a4 + " 00 00 00 FF FF FF FF";
            }

            if (textBoxA3.Text.Length >= 4) 
            {
                textBoxOutput.Text = chHex + " 00 00 00 0" + numAttire + " 00 00 00 " + a1 + " 00 00 00 FF FF FF FF " + a2 + " 00 00 00 FF FF FF FF " + a3 + " 00 00 FF FF FF FF " + a4 + " 00 00 00 FF FF FF FF";
            }

            if (textBoxA4.Text.Length >= 4) 
            {
                textBoxOutput.Text = chHex + " 00 00 00 0" + numAttire + " 00 00 00 " + a1 + " 00 00 00 FF FF FF FF " + a2 + " 00 00 00 FF FF FF FF " + a3 + " 00 00 00 FF FF FF FF " + a4 + " 00 00 FF FF FF FF";
            }

            if (textBoxA3.Text == "00" && textBoxA2.Text.Length >= 4)
            {
                textBoxOutput.Text = chHex + " 00 00 00 0" + numAttire + " 00 00 00 " + a1 + " 00 00 00 FF FF FF FF " + a2 + " 00 00 FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
            }

            if (textBoxA3.Text == "00" && textBoxA2.Text.Length >= 2)
            {
                textBoxOutput.Text = chHex + " 00 00 00 0" + numAttire + " 00 00 00 " + a1 + " 00 00 00 FF FF FF FF " + a2 + " 00 00 00 FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
            }

            int fourBytetoHex = Convert.ToInt32(textBoxCHNumber.Text);
            string fourByte = fourBytetoHex.ToString("X");
            if (fourByte.Length == 3)
            {
                fourByte = "0" + fourByte;
                textBoxOutput.Text = fourByte + " 00 00 0" + numAttire + " 00 00 00 " + a1 + " 00 00 00 FF FF FF FF " + a2 + " 00 00 00 FF FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";


            }



        }
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        if (textBoxAttireNumber.Text == "2")
        {
            textBoxA2.Enabled = true;
            textBoxA3.Enabled = false;
            textBoxA4.Enabled = false;
        }
        else
        {
            textBoxA2.Enabled = false;
        }

        if (textBoxAttireNumber.Text == "3")
        {
            textBoxA2.Enabled = true;
            textBoxA3.Enabled = true;
            textBoxA4.Enabled = false;
        }
        else
        {
            textBoxA3.Enabled = false;
        }

        if (textBoxAttireNumber.Text == "4")
        {
            textBoxA2.Enabled = true;
            textBoxA3.Enabled = true;
            textBoxA4.Enabled = true;
        }

        else
        {
            textBoxA4.Enabled = false;
        }






    }

    private void textBoxA2_TextChanged(object sender, EventArgs e)
    {

    }

    private void buttonWarning_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Credits:\nCredit goes to BrienJ, Tekken57, NikeTheBike, and UndertakerWLF, people who I'm proud to call frieds, they really encouraged and helped me out when I said I wanted to start programming. Especially Tekken, who helped me out with figuring out hex conversion as well.\n\nThis is the first program I've ever made 100% using my knowledge and google, no tutorials, just my basic understanding of C#. So if you find bugs, please forgive me, I'm only a beginner :)");
    }

    private void textBoxA3_TextChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBoxAttireNumber.Enabled = false;
        textBoxA1.Enabled = false;
        textBoxA2.Enabled = false;
        textBoxA3.Enabled = false;
        textBoxA4.Enabled = false;
    }
  }
}

如果这不是发布代码的正确方式,我深表歉意。

【问题讨论】:

  • 改变字节顺序就是把“ABCD”变成“DCBA”。
  • 好的,感谢您的清理。我想在这种情况下我需要交换字节,而不是玩字节序。谢谢。

标签: c# byte swap


【解决方案1】:

欢迎来到 SO。下次尽量不要分享你所有的代码,而只分享有问题/重要的部分。

为您的问题。如果你有一个名为num的整数,如果num小于或等于4095,那么你需要在开头添加“0”,如果num小于或等于255,那么你需要添加“00”和如果小于或等于 15,则添加“000”。使用代码:

if(num <= 15)
{
   textBox1.Text = "0";
}
else
{
   if(num <= 255)
   {
      textBox1.Text = "00";
   }
   else
   {
      if(num <= 4095)
      {
         textBox1.Text = "000";
      }
   }
}

textBox1.Text += num;

如果您只需要将前两个字符与最后一个字符交换,您可以使用:

textBox1.Text = textBox1.Text.Substring(2) + textBox1.Text.Substring(0,2);

【讨论】:

  • 感谢您的欢迎,下次我一定会牢记这一点。嗯......我不确定你是理解错了还是我理解你穿了。基本上,如果我输入“347”,我的程序将显示 015B。我希望它改为显示 5B01,希望可以解决问题,因为我认为我在原始帖子中解释得不够好。谢谢。
  • 是的!效果很好!非常感谢您帮助我,“No Idea For Name”信用已在我的程序的 about 部分中添加给您 :) 干杯!
猜你喜欢
  • 2021-08-07
  • 2022-01-22
  • 2015-05-09
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多