none
String analysis RRS feed

  • Question

  • Hello,
    message:  PANEL_CREATEProg1720180518144115<PanelCreate tokens="4" material="TV" pid_size="1" pid="?" /> 
    received: PANEL_CREATEProg1720180518144115ACK<PanelDetails id="11" mid_1="11" tokens="3" />

    I have to create this string and analyze it on receipt.
      Is with char(2) and char(3) and with group separator.  (see picture)

    ASCII 2    ASCII 3
    STATE

         ACK       or

         NAK

    The last one is an XML element abstract.

     
    Which concept was well suited here?
    Does anyone have an example? Thanks in advance.
    With best regards Markus

    Tuesday, July 10, 2018 4:37 PM

All replies

  • I expect you'll want to use String.Split with the group separator character to divide each string into its component parts.  Then you can check to see if the 4th field of the response is ACK or NAK, and you can go process the XML from the 5th field.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Wednesday, July 11, 2018 7:25 AM
  • Hi Markus Freitag,

    Thank you for posting here.

    According to your description, I do not know what you really want. Please provide more details.

    The data below is different with the data in picture. What is the char(2) and char(3) mean?

    message:  PANEL_CREATEProg1720180518144115<PanelCreate tokens="4" material="TV" pid_size="1" pid="?" /> 
    received: PANEL_CREATEProg1720180518144115ACK<PanelDetails id="11" mid_1="11" mid_1="11" tokens="3" />

    If you want to create XML element abstract, you could try the code below.

       //create xml
                //var xmlNode = new XElement("xml", new XElement("PanelCreate", new XAttribute("tokens", "4"), new XAttribute("material", "TV"), new XAttribute("pid_size", "1"), new XAttribute("pid", "?")),
                //                                  new XElement("PanelDetails", new XAttribute("id", "11"), new XAttribute("mid_1", "11"), new XAttribute("tokens", "3")));
                //xmlNode.Save("test.xml");
    
                XmlDocument doc = new XmlDocument();
                doc.Load("test.xml"); 
    
                XmlNodeList orderNodes = doc.SelectNodes("/xml");
    
                foreach (XmlNode node in orderNodes)
                {
                    var s = node.InnerXml;
    
                }

    Does the another part of the string could be added directly? And what is the STATE ACK OR NAK mean?

    We need more details.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 11, 2018 8:16 AM
    Moderator
  • We need more Details.

    Hello Wendy,

    I have to send a message via string via socket.
    There are identifier at the beginning and end and group separator for each variable.
    How can I convert the characters correctly, what do I have to consider?
    -----
    I receive a string must save these variables. Maybe with split, see my code.
    The last one is just an XML element. How do I read out the attributes?
    string start2 = char.ConvertFromUtf32(2);
    string end2 = char.ConvertFromUtf32(3);
    string group2 = char.ConvertFromUtf32(4);
    
    
    byte[] vals = { 0x02, 0x03, 0x04 };
    string str = BitConverter.ToString(vals);
    
    byte bStart = 2;
    byte bEnd = 3;
    byte bGroup = 4;
    
    string start = String.Format("{0}", (char)2); // (char)0x02;  //$"{bStart}"; //.ToString();
    string end = $"{(char)3}";
    string group = $"{(char)4}";
    
    string prog = "Prog1";
    string order = "2220180518144115";
    string elementXMLInfo = "<PanelCreate tokens=\"4\" material=\"TV\" pid_size=\"1\" pid=\"?\" />";
    string message = start + "PANEL_CREATE" + group + prog + group + order + group + elementXMLInfo + end;
    
    
    // I saw here not the special control ASCII chars
    
    string received = "PANEL_CREATEProg1720180518144115ACK<PanelDetails id=\"11\" mid_1=\"11\" tokens=\"3\" />";
    
    string[] parts = received.Split(new string[] { start, end, group }, StringSplitOptions.RemoveEmptyEntries);
    
    string partsWithNewSeparator = string.Join("|", parts);
    I have to create this string and analyze it on receipt.
      Is with char(2) and char(3) and with group separator.
     
    The last one is an XML element abstract.
    Which concept was well suited here?
     Thanks in advance.
    With best regards Markusok?


    Wednesday, July 11, 2018 4:32 PM
  • Remember that C# Strings are stored in Unicode.  ASCII control characters tend not to survive multiple conversions between Unicode and ASCII.  You should consider using a byte array for the entire transfer instead of strings.  You can look for the control characters and convert the sections in between to strings.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, July 12, 2018 7:04 AM
  • Hello,
    Thanks, but what is the right way?
    Is the same now, isn't?
    Can you give me an example, please?
    byte[] threeItems = new byte[] { 0x02, 0x03, 0x04 };
    int pointer = 0;
    byte[] buffer = new byte[1000];
    //Array.Clear(buffer, 0, buffer.Length);
    Buffer.BlockCopy(threeItems, 0, buffer, pointer, 1);
    pointer++;
    string command = "PANEL_CREATE";
    Buffer.BlockCopy(Encoding.UTF8.GetBytes(command), 0, buffer, pointer, command.Length);
    pointer += command.Length;
    Buffer.BlockCopy(threeItems, 2, buffer, pointer, 1);
    pointer++;
    Buffer.BlockCopy(Encoding.UTF8.GetBytes(prog), 0, buffer, pointer, prog.Length);
    pointer += prog.Length;
    Buffer.BlockCopy(threeItems, 2, buffer, pointer, 1);
    pointer++;
    Buffer.BlockCopy(Encoding.UTF8.GetBytes(order), 0, buffer, pointer, order.Length);
    pointer += order.Length;
    Buffer.BlockCopy(threeItems, 2, buffer, pointer, 1);
    pointer++;
    Buffer.BlockCopy(Encoding.UTF8.GetBytes(elementXMLInfo), 0, buffer, pointer, elementXMLInfo.Length);
    pointer += elementXMLInfo.Length;
    Buffer.BlockCopy(threeItems, 1, buffer, pointer, 1);
    pointer++;
    message = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
    //Array.Clear(buffer, 0, buffer.Length);
    //Array.Copy(threeItems, 0, buffer, buffer.Length, 1);


    What is different between Array copy and block copy?
    What is better?
    Is there a way to get the buffer length up to 0?

    t1

    z6


    byte[] buffer = new byte[1000];
    Can I dynamically define the buffer length? Is depend from the message.
    With best regards Markus

    Thursday, July 12, 2018 4:41 PM
  • What you're doing looks like the right path.  You're wasting a little memory, but I'm not sure it's all that important.  You COULD use a List<byte> instead of an array and expand the list piece by piece, then do toArray at the end to turn it into an array for transmitting to the serial port.

    HOWEVER, you don't want that Encoding.UTF8.GetString at the end.  That's the whole point.  You can't allow this to become a C# string at any point, because the non-ASCII characters will be messed up.  You need it to stay a byte array, and you need to transmit it as a byte array.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, July 13, 2018 12:42 AM
  • >What you're doing looks like the right path.

    Hello,

    ok, thanks. But can you give me a short example. So many possibilities.

    Regards Markus



    Friday, July 13, 2018 2:48 PM