c# equal strings not considered equal

244 views Asked by At

I have problem with monotorrent in C#. It drop peers, because the peer's ID in the handshake is not equal to the given tracker.

The problematic code block looks like this:

    if (id.Peer.PeerId != message.PeerId)
    {
        Logger.Log(id.Connection, "HandShake.Handle - Invalid peerid");

        //Here is my debug:

        string hex = BitConverter.ToString(Encoding.Default.GetBytes(id.Peer.PeerId));
        Console.WriteLine("1: {0}", hex);
        string hex1 = BitConverter.ToString(Encoding.Default.GetBytes(message.PeerId));
        Console.WriteLine("2: {0}", hex1);
        if (hex == hex1)
        {
            Console.WriteLine("hex eq");
        }
        if (id.Peer.PeerId.Equals(message.PeerId))
        {
            Console.WriteLine("string eq");
        }
        throw new TorrentException("Supplied PeerID didn't match the one the tracker gave us");
    }

It returns something like this:

1: 2D-42-54-37-39-32-30-2D-78-3F-3F-3F-12-4C-24-3F-71-3F-3E-68
2: 2D-42-54-37-39-32-30-2D-78-3F-3F-3F-12-4C-24-3F-71-3F-3E-68
hex eq
1: 2D-42-54-37-39-32-30-2D-78-3F-3F-3F-12-4C-24-3F-71-3F-3E-68
2: 2D-42-54-37-39-32-30-2D-78-3F-3F-3F-12-4C-24-3F-71-3F-3E-68
hex eq

Why are strings, which are fully identical in hex, not considered equal?

Update: The tracker returns response to announce request (get peers) encoded in CP1251. Could this cause a problem?

Update2: I think the reason is som encoding flags. I added this:

            var str = System.Text.Encoding.Default.GetString(Encoding.Default.GetBytes(id.Peer.PeerId));
            var str1 = System.Text.Encoding.Default.GetString(Encoding.Default.GetBytes(message.PeerId));

            if (id.Peer.PeerId.Equals(message.PeerId))
            {
                Console.WriteLine("string eq");
            }
            if (str.Equals(str1))
            {
                Console.WriteLine("str eq str1");
            }

and got this:

1: 2D-55-54-33-34-32-30-2D-7E-7D-01-4B-3F-79-3F-3F-3F-1A-73-3F
2: 2D-55-54-33-34-32-30-2D-7E-7D-01-4B-3F-79-3F-3F-3F-1A-73-3F
hex eq
str eq str1
1: 2D-55-54-33-33-32-30-2D-5F-76-11-3F-3F-6E-3F-0A-3F-54-62-3F
2: 2D-55-54-33-33-32-30-2D-5F-76-11-3F-3F-6E-3F-0A-3F-54-62-3F
hex eq
str eq str1

very interesting!

2

There are 2 answers

1
Michael On

When it comes to string comparison, I always use Trim() both Strings and use String.Equals() Method. If the comparison is not case sensitive, then I also use ToLower() method, before the comparison starts.

http://msdn.microsoft.com/de-de/library/t4411bks%28v=vs.110%29.aspx

Boolean StringsAreEqual = String.Equals(String1.Trim().ToLower(), String2.Trim().ToLower(), ...)
3
rodrigogq On

Use the following construction to compare strings in C#:

if(String.Compare(id.Peer.PeerId, message.PeerId, StringComparison. InvariantCultureIgnoreCase) != 0) 
{ 

    // strings as different
}

You can switch the StringComparison as you need. Take a look at these options: http://msdn.microsoft.com/en-us/library/system.stringcomparison%28v=vs.110%29.aspx