Problems with GetString?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    #1

    Problems with GetString?

    Hi,
    Can anyone explain the following behaviour?

    private static int BUFFER_SIZE = 1024;
    private byte [] Buffer = new byte[BUFFER_SIZE];
    private Encoding ASCII = Encoding.ASCII;
    private System.Net.Sock ets.Socket mySocket;

    // Already accepted a connection...
    int Length = mySocket.Receiv e(Buffer);
    String str1 = ASCII.GetString (Buffer);
    String str2 = ASCII.GetString (Buffer, 0, Length);

    bool Same = (str1 == str2)
    // Same is false!!!!!

    // I only found this out because I was trying to open
    // a file using str1 as the filename as follows...
    StreamReader mySR = File.OpenText(s tr1);
    // Gives an error 'illegal characters',
    // whereas
    StreamReader mySR = File.OpenText(s tr2);
    // Works fine!!!

    Both str1 and str2 look exactly the same when debugging.
    Am I missing something or is this a known bug in C#?

    Thanks,
    Simon.
  • Bret Mulvey [MS]

    #2
    Re: Problems with GetString?

    It's a quirk of the debugger display. The length of str1 will be BUFFER_SIZE
    and the length of str2 will be Length. The buffer initially contains all
    0's. To GetString() these are just '\0' characters in the string, but to the
    debugger display a \0 is interpreted as the end of the string.

    "Simon" <anonymous@disc ussions.microso ft.com> wrote in message
    news:05a701c3a3 4d$fc1ceff0$a50 1280a@phx.gbl.. .[color=blue]
    > Hi,
    > Can anyone explain the following behaviour?
    >
    > private static int BUFFER_SIZE = 1024;
    > private byte [] Buffer = new byte[BUFFER_SIZE];
    > private Encoding ASCII = Encoding.ASCII;
    > private System.Net.Sock ets.Socket mySocket;
    >
    > // Already accepted a connection...
    > int Length = mySocket.Receiv e(Buffer);
    > String str1 = ASCII.GetString (Buffer);
    > String str2 = ASCII.GetString (Buffer, 0, Length);
    >
    > bool Same = (str1 == str2)
    > // Same is false!!!!!
    >
    > // I only found this out because I was trying to open
    > // a file using str1 as the filename as follows...
    > StreamReader mySR = File.OpenText(s tr1);
    > // Gives an error 'illegal characters',
    > // whereas
    > StreamReader mySR = File.OpenText(s tr2);
    > // Works fine!!!
    >
    > Both str1 and str2 look exactly the same when debugging.
    > Am I missing something or is this a known bug in C#?
    >
    > Thanks,
    > Simon.[/color]


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: Problems with GetString?

      Simon <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
      > Can anyone explain the following behaviour?
      >
      > private static int BUFFER_SIZE = 1024;
      > private byte [] Buffer = new byte[BUFFER_SIZE];
      > private Encoding ASCII = Encoding.ASCII;
      > private System.Net.Sock ets.Socket mySocket;
      >
      > // Already accepted a connection...
      > int Length = mySocket.Receiv e(Buffer);
      > String str1 = ASCII.GetString (Buffer);
      > String str2 = ASCII.GetString (Buffer, 0, Length);
      >
      > bool Same = (str1 == str2)
      > // Same is false!!!!![/color]

      Yes - consider that Length might be (say) 10. Then one of your strings
      will have length 10, and one will have length 1024. You should only be
      decoding the bytes you've actually received.

      --
      Jon Skeet - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Simon

        #4
        Re: Problems with GetString?

        Thanks for this! I come from a C/C++ background so I'm
        still thinking in strcmp, strlen etc and therefore think a
        \0 means the end of the string!
        [color=blue]
        >-----Original Message-----
        >It's a quirk of the debugger display. The length of str1[/color]
        will be BUFFER_SIZE[color=blue]
        >and the length of str2 will be Length. The buffer[/color]
        initially contains all[color=blue]
        >0's. To GetString() these are just '\0' characters in the[/color]
        string, but to the[color=blue]
        >debugger display a \0 is interpreted as the end of the[/color]
        string.[color=blue]
        >
        >"Simon" <anonymous@disc ussions.microso ft.com> wrote in[/color]
        message[color=blue]
        >news:05a701c3a 34d$fc1ceff0$a5 01280a@phx.gbl. ..[color=green]
        >> Hi,
        >> Can anyone explain the following behaviour?
        >>
        >> private static int BUFFER_SIZE = 1024;
        >> private byte [] Buffer = new byte[BUFFER_SIZE];
        >> private Encoding ASCII = Encoding.ASCII;
        >> private System.Net.Sock ets.Socket mySocket;
        >>
        >> // Already accepted a connection...
        >> int Length = mySocket.Receiv e(Buffer);
        >> String str1 = ASCII.GetString (Buffer);
        >> String str2 = ASCII.GetString (Buffer, 0, Length);
        >>
        >> bool Same = (str1 == str2)
        >> // Same is false!!!!!
        >>
        >> // I only found this out because I was trying to open
        >> // a file using str1 as the filename as follows...
        >> StreamReader mySR = File.OpenText(s tr1);
        >> // Gives an error 'illegal characters',
        >> // whereas
        >> StreamReader mySR = File.OpenText(s tr2);
        >> // Works fine!!!
        >>
        >> Both str1 and str2 look exactly the same when debugging.
        >> Am I missing something or is this a known bug in C#?
        >>
        >> Thanks,
        >> Simon.[/color]
        >
        >
        >.
        >[/color]

        Comment

        • Guest's Avatar

          #5
          Re: Problems with GetString?

          Thanks for this! I come from a C/C++ background so I'm
          still thinking in strcmp, strlen etc and therefore think a
          \0 means the end of the string!
          [color=blue]
          >-----Original Message-----
          >Simon <anonymous@disc ussions.microso ft.com> wrote:[color=green]
          >> Can anyone explain the following behaviour?
          >>
          >> private static int BUFFER_SIZE = 1024;
          >> private byte [] Buffer = new byte[BUFFER_SIZE];
          >> private Encoding ASCII = Encoding.ASCII;
          >> private System.Net.Sock ets.Socket mySocket;
          >>
          >> // Already accepted a connection...
          >> int Length = mySocket.Receiv e(Buffer);
          >> String str1 = ASCII.GetString (Buffer);
          >> String str2 = ASCII.GetString (Buffer, 0, Length);
          >>
          >> bool Same = (str1 == str2)
          >> // Same is false!!!!![/color]
          >
          >Yes - consider that Length might be (say) 10. Then one of[/color]
          your strings[color=blue]
          >will have length 10, and one will have length 1024. You[/color]
          should only be[color=blue]
          >decoding the bytes you've actually received.
          >
          >--
          >Jon Skeet - <skeet@pobox.co m>
          >http://www.pobox.com/~skeet
          >If replying to the group, please do not mail me too
          >.
          >[/color]

          Comment

          Working...