Results 1 to 19 of 19

Thread: [RESOLVED] XOR data from input string

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. 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:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    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
    Code:
    byte i=0
    inside the for loop. That should be OK?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: XOR data from input string

    ok, then:
    Code:
    byte j=0;
    
    // loop through bytes array
    for (int i = 0; i < bytes.Length; i++)

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: XOR data from input string

    Something like:
    Code:
    j ^=bytes[i]
    That should xor the different bytes in the array inside the loop, right?

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    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

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    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.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.");
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: XOR data from input string

    Ran the code. b1 = b2.
    But why is it shown in decimal now?

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    Re: XOR data from input string

    OK, and again thank you for spending so much time on it

  19. #19

    Thread Starter
    New Member
    Join Date
    Jul 2010
    Posts
    11

    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
  •  



Click Here to Expand Forum to Full Width