Reputation: 35
I'll start off by saying that I'm very new to this place and programming, as well, I've only been programming for two or three days!! Well, I'm working on a program, it basically takes information from the user and prints out a 28 byte hexadecimal code that the user needs. It's complicated to explain :P Anyway, I've run into a problem, and because I'm new, have no idea how to get by it. See, I basically want to tell my program that "Hey, convert the value in textBox1 to it's hexadecimal equivalent, but, if the hexadecimal equivalent is a 3 byte value, add a "0" in front of it and swap change the endianess" I've got all of that figured out, the converting to hex, the 3 byte thing, but, the the endianess part I don't even know where to start. I tried searching, but I really didn't understand much, so please try to explain in as much detail as possible.
The hexadecimal value I converted into a string so that it could be displayed in a text box.
Oh and you know what, I wasn't going to post this before, because it's so messy, but here's my code, it'll only help you in helping me :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;
}
}
}
My apologies if that's not the correct way to post code.
Upvotes: 2
Views: 741
Reputation: 11577
welcome to SO. next time try not to share all your code, but only the problematic/important part.
for your question. if you have am integer called num
, if num is less or equal 4095, then you'll need to add "0" at the start, if num is less or equal 255 then you'll need to add "00" and if it's less or equal 15 then add "000". use the code:
if(num <= 15)
{
textBox1.Text = "0";
}
else
{
if(num <= 255)
{
textBox1.Text = "00";
}
else
{
if(num <= 4095)
{
textBox1.Text = "000";
}
}
}
textBox1.Text += num;
if you just need to swap the first two characters with the last to you can use:
textBox1.Text = textBox1.Text.Substring(2) + textBox1.Text.Substring(0,2);
Upvotes: 1