Understanding bits/bytes with hex
<p>I am unable to figure out how this was found. I am looking to understand how this person is finding this stuff out and how to do it automatically in C# (helps to understand it but also get code). The thread for where this is at is <a href="http://forums.radioreference.com/project-25-technologies/228247-p25-control-messages.html" rel="nofollow">http://forums.radioreference.com/project-25-technologies/228247-p25-control-messages.html</a> and the post is <a href="http://forums.radioreference.com/project-25-technologies/228247-p25-control-messages.html#post1663780" rel="nofollow">Post #8</a>. Please help me understand bytes and bits. The hex code (I believe to be hex) comes in the format as follows from a police scanner:</p>

<pre><code>P25:T0001:80000400FD0008001B5F3A09
</code></pre>

<p>The quote from the forum is as follows (each two from the above make an octet as its called)</p>

<blockquote>
  <p>80 00 04 00 FD 00 08 00 1B 5F 3A 09</p>
  
  <p>-</p>
  
  <p>Taking the lower 6 bits of the first byte (byte 0) gives you a value of 0, which is a group voice grant</p>
  
  <p>Second byte is the Manufacturer id. Value of 00 is a standard packet</p>
  
  <p>Talkgroup ID is bytes 5 &amp; 6.</p>
  
  <p>Radio ID is bytes 7, 8 &amp; 9</p>
  
  <p>Bytes 10 &amp; 11 are 16 bit CRC.</p>
</blockquote>

<p>The code that I currently have gives me the following output which doesn't seem to be correct. The radio ID is 7007 which I don't see how bytes 7, 8, 9 (which are 08 00 1B) gets 7007.</p>

<blockquote>
  <p>128 (0), 0 (0), 4 (4), 0 (0), 253 (61), 0 (0), 8 (8), 0 (0), 27 (27), 95 (31), 58 (58), 9 (9)</p>
</blockquote>

<p>and I have no idea what this code even is:</p>

<pre><code>String SelectedText = lstboxMessages.SelectedItem.ToString();
String[] SplittedText = SelectedText.Replace("\\r\\n", "").Replace("\r\n", "").Split(new String[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
int[] Octets = new int[12];

SplittedText[2] = "80000400FD0008001B5F3A09";

Octets[0] = Convert.ToInt32(SplittedText[2].Substring(0, 2), 16);
Octets[1] = Convert.ToInt32(SplittedText[2].Substring(2, 2), 16);
Octets[2] = Convert.ToInt32(SplittedText[2].Substring(4, 2), 16);
Octets[3] = Convert.ToInt32(SplittedText[2].Substring(6, 2), 16);
Octets[4] = Convert.ToInt32(SplittedText[2].Substring(8, 2), 16);
Octets[5] = Convert.ToInt32(SplittedText[2].Substring(10, 2), 16);
Octets[6] = Convert.ToInt32(SplittedText[2].Substring(12, 2), 16);
Octets[7] = Convert.ToInt32(SplittedText[2].Substring(14, 2), 16);
Octets[8] = Convert.ToInt32(SplittedText[2].Substring(16, 2), 16);
Octets[9] = Convert.ToInt32(SplittedText[2].Substring(18, 2), 16);
Octets[10] = Convert.ToInt32(SplittedText[2].Substring(20, 2), 16);
Octets[11] = Convert.ToInt32(SplittedText[2].Substring(22, 2), 16);

String strSpecial = Convert.ToString(Convert.ToInt32(SplittedText[2].Substring(0, 2), 16), 2).PadLeft(8, '0');

String binOpCode = strSpecial.Substring(2, 6);
int OpCode = Convert.ToInt32(binOpCode, 2);

String strReturnMessage = ""; //= SplittedText[2] + " = ";

for (int i = 0; i &lt; Octets.Count(); i++)
{
    if (i != 0)
    {
        strReturnMessage += ", ";
    }

    strReturnMessage += Convert.ToString(Octets[i]);
    strReturnMessage += " (" + BinaryToInt(Convert.ToString(Convert.ToInt32(SplittedText[2].Substring((i * 2), 2), 16), 2).PadLeft(8, '0')).ToString() + ")";
}

txtLastMessage.Text = strReturnMessage;
</code></pre>

<p>and BitToInt() is</p>

<pre><code>private int BinaryToInt(String strBinary)
{
    String binOpCode = strBinary.Substring(2, 6);
    return Convert.ToInt32(binOpCode, 2);
}
</code></pre>
