我首先要说的是,我对这个地方和编程都很陌生,我也只是编程了两三天! 好吧,我正在研究一个程序,它基本上从用户那里获取信息并打印出用户需要的28字节十六进制代码。解释起来很复杂:P 无论如何,我遇到了一个问题,因为我是新手,不知道如何通过它。看,我基本上想告诉我的程序“嘿,将textBox1中的值转换为它的十六进制等效值,但是,如果十六进制等效值是3字节值,则在它前面添加一个”0“并交换更改endianess”我已经弄清楚所有这一切,转换为十六进制,3字节的东西,但是,endianess部分我甚至不知道从哪里开始。我试着搜索,但我真的不太懂,所以请尽量详细解释。
我转换为字符串的十六进制值,以便它可以显示在文本框中。
哦,你知道吗,我之前不会发布这个,因为它太乱了,但这是我的代码,它只会帮助你帮助我: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;
}
}
}
如果这不是发布代码的正确方法,我很抱歉。
答案 0 :(得分:1)
你的问题。如果你有一个名为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);