gpt4 book ai didi

c# - 交换字节顺序/字节顺序

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:03 25 4
gpt4 key购买 nike

首先我会说我对这个地方和编程很陌生,而且,我只编程了两三天!!好吧,我正在开发一个程序,它基本上从用户那里获取信息并打印出用户需要的 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;
}
}
}

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

最佳答案

欢迎来到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);

关于c# - 交换字节顺序/字节顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18680778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com