Visual C# Developer Center > Visual C# Forums > Visual C# General > how do I eliminate or not introduce "\0\b" for this serial port rs232 app?
Ask a questionAsk a question
 

General Discussionhow do I eliminate or not introduce "\0\b" for this serial port rs232 app?

  • Friday, November 06, 2009 5:11 PMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    I'm porting code from vb.net to c#  I want to adhere to the value of 'packet' as the vb code illustrates.
    ' vb.net
    
     Dim packet As String
    
       ' packet value at this point of the code execution = "ÿÿÿÿþÿü"
    
     packet = packet & Chr((Len(pdu) + 8) / 256)     'pdu = ""
    
     packet = packet & Chr((Len(pdu) + 8) Mod 256)
    
        ' packet as seen in Quickwatch window  = "ÿÿÿÿþÿü"
    
    

    the c# code below is my 'best guess' for the equivalent vb.net code.  I want to eliminate the "\0\b" characters.

    //c#	
    
    
    
    StringBuilder packet = new StringBuilder(""); 
    
      // packet value at this point of the code execution = "ÿÿÿÿþÿü"
    
    packet.Append((char)((pdu.Length + 8)/256 ));   //string pdu = ""
    
    packet.Append((char)((pdu.Length + 8) % 256));
    
      //packet as seen in QuickWatch window =  "ÿÿÿÿþÿü\0\b"
    
    
    
    

All Replies

  • Monday, November 09, 2009 10:05 AMGuang-Ming Bian - MSFTMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi hazz,

    Firstly, sorry for my late reply.

    As far as I know, c# result is the expected result. Why not you want to eliminate it, if you really want to remove it, we can use String.Substring to remove it.

    For how to use String.Substring method, please click following thread:
    http://msdn.microsoft.com/en-us/library/aka44szs.aspx

    Have a nice day.




    Best regards,
    Guang-Ming Bian - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact
    msdnmg@microsoft.com





    Please remember to mark the replies as answers if they help and unmark them if they provide no help

  • Monday, November 09, 2009 5:15 PMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you for your reply and suggestion.
    Although I think i need to learn more about binary serial packetizing and character/byte conversion utilities, perhaps byte order (most signficant bit (big endian)  and least significant byte or bit (little endian) concepts, for now my question is

    How do I write the correct code to remove "\b" from a given stringbuilder object.

    packet.Remove((packet.ToString()).Substring(

    "\b\0")  

    is my best guess so far.

    Thank you for any help.
    Greg

  • Monday, November 09, 2009 8:00 PMChris Hannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It would appear that the Quickwatch window in VB.Net does not like showing you the explicit char value in your string, whereas C# does. Here is an example.

    In VB.Net

    Dim s As String
    Dim c As Char
    s = "test"
    c = Environment.NewLine
    s = s & c

    The value of s, in the Quickwatch window, is "test ".

    In C#

    string s = "test";
    char c = Environment.NewLine;
    s += c;

    The value of s, in the Quickwatch window, is "test\r\n".

    This hardly means that you should strip out the special characters, it just means that C# shows you the value and VB.Net does not. In practice, they are identical.

    In your case, both lines produce non-printable chars which do not show up in VB.Net Quickwatch window. The division by 256 produces a null (\0), which is registered as Nothing in VB.Net and is not shown. The mod 256 produces a backspace (\b) and is not shown either. The code appears to be doing the same thing in both cases, with the exception that the original is using Chr() instead of ChrW() and might give you weird conversions if you direct cast through char instead of using System.Text.ASCIIEncoding.Default or something similar.
  • Monday, November 09, 2009 11:08 PMhazz Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank  you Chris !