|
-
Jul 24th, 2010, 02:23 PM
#1
Thread Starter
New Member
[RESOLVED] XOR data from input string
Hi,
I have made a simple windows form, with a input (textBox1), a Calculate button (button1), and another textBox2 for the result.
But I'am quite new to this Windows form and C#. But eager to learn
I would like to be able to insert a hex string like this: 41, 4d, 21, 04
And then be able to calculate the XOR value (41^4d^21^04) when pressing the Calculate button
Could anyone help me, or point me in the right direction?
This is what I have so far:
private void button1_Click(object sender, EventArgs e)
{
// comma delimited hex string from userinput textbox1
string commaDelimited = textBox1.Text;
// separate individual items between commas
string[] value = commaDelimited.Split(new char[] { ',' });
foreach (string number in value)
{
Last edited by bbhe; Jul 24th, 2010 at 06:27 PM.
-
Jul 24th, 2010, 10:02 PM
#2
Re: XOR data from input string
Code:
// separate individual items between commas
string[] value = commaDelimited.Split(new char[] { ',' });
That's cool but I would suggest a better name than 'value' for the array. It's not really a single value, is it? At the least the name should be 'values', but something like 'hexValues' might be more appropriate. Also, there's an overload of Split that takes a single char parameter, so there's no need to create the char array.
csharp Code:
string[] hexValues = textBox1.Text.Split(',');
I would then suggest using the Array.ConvertAll method to convert that string array to an array of numbers. If you're only accepting 2-digit hex values then those numbers can be bytes, or you can use a larger type if required.
csharp Code:
byte[] bytes = Array.ConvertAll(hexValues, s => Convert.ToByte(s.Trim()));
You can now loop through that array of bytes and do whatever. You would declare a byte variable and initialise it to zero, then XOR each element in the array with it in the loop.
-
Jul 25th, 2010, 12:05 AM
#3
Thread Starter
New Member
Re: XOR data from input string
Hi jmcilhinney,
Thanks for your advise. That really helped me alot.
I have made some adjustment, as you recommended. But I'am not sure how to write the line, where the xor calculation should be done.
I have made the loop to loop through the new bytes array, but how can I xor the data?
Could you give me an example?
-
Jul 25th, 2010, 12:16 AM
#4
Re: XOR data from input string
Look first, ask questions later. Most information you need is already available. You just have to look for it.
http://www.google.com.au/search?q=xo...ient=firefox-a
-
Jul 25th, 2010, 02:02 PM
#5
Thread Starter
New Member
Re: XOR data from input string
I have spend almost the entire day searching google for something that I could use. But without luck.
I have tried with the following:
private void button1_Click(object sender, EventArgs e)
{
// comma delimited hex string from userinput textbox1
string commaDelimited = textBox1.Text;
// separate individual items between commas
string[] hexValues = textBox1.Text.Split(',');
// converting string array (hexValues) to byte array
byte[] bytes = Array.ConvertAll(hexValues, s => Convert.ToByte(s.Trim()));
byte[] NewValue = {0};
// loop through bytes array
for (byte i = 0; i < bytes.Length; i++)
{
NewValue[i] ^= bytes[i];
}
But I think that it is wrong. I have just started to learn C# two days ago, so perhaps I just have to wait until I learn some more basic stuff.
I'am currently reading Sams Teach Yourself C# in 21 days. Just to get an idea. There are alot of small programs that I think I can learn alot from.
I just thought that it would be easy to do. But thank you for having taken the time to help me.
-
Jul 25th, 2010, 07:07 PM
#6
Re: XOR data from input string
OK, let's take a look at what I posted:
You would declare a byte variable and initialise it to zero
where have you done that? This:
Code:
byte[] NewValue = {0};
is not it. That is creating a byte array. I didn't say anything about an array.
-
Jul 25th, 2010, 08:14 PM
#7
Thread Starter
New Member
Re: XOR data from input string
That line:
Code:
byte[] NewValu = {0};
was just some kind of experiment. Should be deleted.
I have declared a
inside the for loop. That should be OK?
-
Jul 25th, 2010, 08:20 PM
#8
Re: XOR data from input string
No, not inside the loop. The idea is that that value is the base and then you update it by XORing each iteration of the loop. You only want to initialise it once, before the loop.
-
Jul 25th, 2010, 08:32 PM
#9
Thread Starter
New Member
Re: XOR data from input string
ok, then:
Code:
byte j=0;
// loop through bytes array
for (int i = 0; i < bytes.Length; i++)
-
Jul 25th, 2010, 08:39 PM
#10
Re: XOR data from input string
Right. Now you do the rest of what I already posted previously:
You would declare a byte variable and initialise it to zero, then XOR each element in the array with it in the loop.
-
Jul 25th, 2010, 08:51 PM
#11
Thread Starter
New Member
Re: XOR data from input string
Something like:
That should xor the different bytes in the array inside the loop, right?
-
Jul 25th, 2010, 09:05 PM
#12
Thread Starter
New Member
Re: XOR data from input string
I've just aded:
Code:
this.listBox1.Items.Add(j);
to see if it looked ok. The output to the listbox was almost OK.
I used input 41,21 and got 41 and 60 in the listbox. XOR of 41 and 21 is 60!!
But it still need some adjustments.
If I enter 4d,21 it fails with an "unhandled exception" error
-
Jul 25th, 2010, 09:11 PM
#13
Re: XOR data from input string
As you're parsing hexadecimal values, this:
Code:
byte[] bytes = Array.ConvertAll(hexValues, s => Convert.ToByte(s.Trim()));
should be this:
Code:
byte[] bytes = Array.ConvertAll(hexValues, s => Convert.ToByte(s.Trim(), 16));
The second parameter specifies the base of the string representation of the number and can be 16, 10, 8 or 2 for hexadecimal, decimal, octal or binary respectively.
-
Jul 25th, 2010, 10:03 PM
#14
Thread Starter
New Member
Re: XOR data from input string
After having changed second parameter to: 16, I now get the result 96 when input is 41,21. It should be 60
But it seems to be close to finished.
If you don't have the time, I'll understand it 100%. You have helped me plenty.
-
Jul 25th, 2010, 10:13 PM
#15
Re: XOR data from input string
Try running this code:
Code:
byte b1 = 0x60;
byte b2 = 96;
MessageBox.Show(b1.ToString(), "Decimal");
MessageBox.Show(b1.ToString("X"), "Hexadecimal");
MessageBox.Show(b2.ToString(), "Decimal");
MessageBox.Show(b2.ToString("X"), "Hexadecimal");
if (b1 == b2)
{
MessageBox.Show("96 and 0x60 are the same number.");
}
-
Jul 25th, 2010, 10:43 PM
#16
Thread Starter
New Member
Re: XOR data from input string
Ran the code. b1 = b2.
But why is it shown in decimal now?
-
Jul 25th, 2010, 10:57 PM
#17
Re: XOR data from input string
A number is a number. Decimal, hexadecimal, etc, are simply different ways to represent numbers. Your numbers are what they are. How you represent then is up to you.
I think what's confusing you is the fact that 41 ^ 21 = 60 and 0x41 ^ 0x21 = 0x60. That's just a coincidence. The code is correct and what it's doing is correct. It's your interpretation of what's happening that is incorrect. Originally, you were parsing the Strings as decimal numbers, so "41, 21" was giving you the decimal numbers 41 and 21. XORing the decimal numbers 41 and 21 gives you the decimal number 60. Now you are parsing the Strings as hexadecimal numbers, so "41, 21" is giving you the hexadecimal numbers 0x41 and 0x21, which are the decimal numbers 65 and 33. XORing the decimal numbers 65 and 33 gives decimal number 96, which is the hexadecimal number 0x60. You need to keep straight in your head what's decimal and what's hexadecimal.
-
Jul 25th, 2010, 11:06 PM
#18
Thread Starter
New Member
Re: XOR data from input string
OK, and again thank you for spending so much time on it
-
Jul 25th, 2010, 11:44 PM
#19
Thread Starter
New Member
Re: XOR data from input string
Done. Now it works perfectly. 
Again, thanks for all the time you have spend. I really apreciate it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|