CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2008
    Location
    VC#2008 Express, .Net V. 3.5
    Posts
    8

    [RESOLVED] Encoding.ASCII Problem...

    I'm currently writing a Server and Client program that communicates over my local network by the TCP Classes. The scenario is Client sends Server a message, Server receives the message, which is encoded in ASCII, decodes it into a string, displays the message in console, then encodes a response to the Client in ASCII, sends it, the client repeats the decode into a string and displays the response to console...

    The problem that I am having is that along with the messages that are being transferred are a bunch of control charters (\0 to be exact).

    Is there anyway that I can stop this from happening or is there a better way to Encode my messages in to bytes and back to strings than Encoding.ASCII.GetBytes()/.GetString() method?

    [EDIT]

    I solved the problem with a hackish workaround
    Code:
    string serverResponse = Encoding.ASCII.GetString(bytes);
    int index = serverResponse.IndexOf("\0");
    serverResponse =  serverResponse.Remove(index);
    
    //use response
    This just seems a lot of redundant string manipulations(Which I know can have a hefty overhead) so I would like to know if there is a better way to get the same result?
    Last edited by bummper3200; December 31st, 2008 at 09:56 PM.

  2. #2
    Join Date
    Apr 2006
    Posts
    220

    Re: Encoding.ASCII Problem...

    The point is that why is the \0 character coming in response and what is its purpose.

  3. #3
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Encoding.ASCII Problem...

    GetString converts the whole byte array - so if you have "hello" in a byte array of length 10 you'll get 5 '\0' at the end.

    You need to tell GetString the range in the byte array to be converted.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Encoding.ASCII Problem...

    you can also try:

    Encoding.BigEndianUnicode.GetBytes()
    Encoding.BigEndianUnicode.GetString()
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  5. #5
    Join Date
    Jun 2008
    Location
    VC#2008 Express, .Net V. 3.5
    Posts
    8

    Re: Encoding.ASCII Problem...

    The problem is pretty much resolved, the string manipulations aren't causing a noticeable slowdown or increased CPU usage so its satisfactory. My programs wont be sending constant messages or subjected to a lot of information transfers so I doubt they will come back to bite me...

    Thank you for your replies.

    bummper3200.

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

Featured