Escape sequences from an integer
<p>I am writing an application to communicate with a serial device and I need to send the length of a string field in the string that is written to the serial port. 
For example <code>"\x00\x27"</code> would be 27 bytes, and if I send this in the string it works as everything is hard coded so I know the length.</p>

<p>Another example is <code>"\x01\x27"</code> would be 127 bytes.</p>

<p>The problem is I need to be able to send variable length string fields. I know I can get the length using </p>

<pre><code>    int lninbyte = System.Text.ASCIIEncoding.ASCII.GetByteCount(data);
</code></pre>

<p>but how do I convert <code>lninbyte</code> into <code>"\x00\x27"</code>?</p>

<pre><code>private void GetBCDDataLength(string data)
{
    int lninbyte = System.Text.ASCIIEncoding.ASCII.GetByteCount(data);
    if (lninbyte &gt; 99)
    {
    }
    else
    {
        BCDlenData = "\x00" + lninbyte.ToString("X");
    }
}
</code></pre>
