# Read binary serial data and parse integers

I'm reading serial data in binary format from a stream, and I'm not happy with how I extract the data because it takes so much operations to extract the binary data from a simple line of binary data.

Each line starts with a character indicating the type of data, and afterwards follow a few 16 bit integers (big endian), followed by a checksum character and a newline.

Here's a sample of what line would be after reading:

line = "F{3x 16 bit int big endian}{checksum character}\n"


This is the simplified code in question:

  byte[] b = new byte;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

// Check that the port is open. If not skip and do nothing
if (SerialPort.IsOpen)
{

// read line and turn string in byte array
byte[] encoded = enc.GetBytes(line);

if (line=='F'){

// copy 16 bit int into byte-array
Array.Copy(encoded, 1, b, 0, 2);
// -> big endian
Array.Reverse(b);

// turn into integer
short value1 = BitConverter.ToInt16(b, 0);

Array.Copy(encoded, 3, b, 0, 2);
Array.Reverse(b);
short value1 = BitConverter.ToInt16(b, 0);

Array.Copy(encoded, 5, b, 0, 2);
Array.Reverse(b);
short value3 = BitConverter.ToInt16(b, 0);
}

}


Is there a smarter way to accomplish the result of my code above?

## Data Acquisition

Is there a smarter way to accomplish the result of my code above?

So, I have another suggestion, to work on top of the suggestions by Heslacher:

Eliminate the BitConverter work. This is easily done with boolean operators (most specifically the shift operators).

short value1 = (short)((short)encoded << 8 | encoded);
short value2 = (short)((short)encoded << 8 | encoded);
short value3 = (short)((short)encoded << 8 | encoded);


Why? It's fast as hell. And this tends to make it more clear what position each value represents. There's no need to place them backwards as you have to with the BitConverter. Looking at this code, it's extremely easy to determine that each pair consists of the high-byte, followed by the low-byte. With the BitConverter, the lowest byte must be followed by the subsequently higher bytes.

Can it really be faster, with all those casts, shifting (<<) and or's (|), and the fact that Microsoft themselves wrote the BitConverter?

You're damn right it can. Run the following programme:

byte[] encoded = new byte;
encoded = 0xF0;
encoded = 0xF1;
encoded = 0xF2;
encoded = 0xF3;
encoded = 0xF4;
encoded = 0xF5;
encoded = 0xF6;

Stopwatch sw1 = new Stopwatch();

short value1 = 0, value2 = 0, value3 = 0;

sw1.Start();
for (int i = 0; i < 100000000; i++)
{
value1 = BitConverter.ToInt16(new byte[] { encoded, encoded }, 0);
value2 = BitConverter.ToInt16(new byte[] { encoded, encoded }, 0);
value3 = BitConverter.ToInt16(new byte[] { encoded, encoded }, 0);
}
sw1.Stop();
Console.WriteLine(sw1.ElapsedTicks.ToString());
sw1.Reset();

Console.WriteLine(value1.ToString("X"));
Console.WriteLine(value2.ToString("X"));
Console.WriteLine(value3.ToString("X"));

sw1.Start();
for (int i = 0; i < 100000000; i++)
{
value1 = (short)((short)encoded << 8 | encoded);
value2 = (short)((short)encoded << 8 | encoded);
value3 = (short)((short)encoded << 8 | encoded);
}
sw1.Stop();
Console.WriteLine(sw1.ElapsedTicks.ToString());
sw1.Reset();

Console.WriteLine(value1.ToString("X"));
Console.WriteLine(value2.ToString("X"));
Console.WriteLine(value3.ToString("X"));


Results:

12956403
F1F2
F3F4
F5F6
1505318
F1F2
F3F4
F5F6


You'll notice that the bitwise-operator method takes 12% of the time the BitConverter method does. And in this situation it makes a great deal of sense. It's easier to reason how the value1, value2 and value3 variables are being filled with the bitwise-operators. It's not so obvious with the BitConverter.

Yes, this is obviously a worst-case environment, you will likely have other bottlenecks and JITer that will influence it, but this at least goes to show that there are other just as good solutions to the same problem. Personally, I've never touched the BitConverter, I always do my own bitwise-arithmetic. To me, it's easier to read and more obvious. To you, it may not be.

I wouldn't suggest going so far as to make a loop. I did just that, and it's actually slower.

short[] myValues = new short;

sw1.Start();
for (int i = 0; i < 100000000; i++)
{
for (int t = 0; t < myValues.Length; t++)
myValues[t] = (short)((short)encoded[1 + t * 2] << 8 | encoded[2 + t * 2]);
}
sw1.Stop();
Console.WriteLine(sw1.ElapsedTicks.ToString());
sw1.Reset();

for (int t = 0; t < myValues.Length; t++)
Console.WriteLine(myValues[t].ToString("X"));


Results:

3759655
F1F2
F3F4
F5F6


Twice the time the non-loop method took. With a dynamic List you can expect that it will be even slower. (Haven't done any experiments with that method, but one can estimate it would take longer.)

## Commenting // when is too much and not enough

Another suggestion, based on his answer: put a comment on the while(SerialPort.BytesToRead > 50){ line describing why we only want it if there are more than 50 bytes left to read. From your current code it's impossible to reason why.

Comments are very powerful; and though I am very liberal with my comments (placing one every 2-10 lines), you don't have to be. But they should be often enough that you don't have to examine a 200+ line file to get the relevant bits. Add minor information on where these values are used, what are they for, if you are using indices like this, then markup what each index represents. You don't have to go ape-s*** on them. Just describe what it is you are doing, and why.

• Great you changed from comment to a very good answer. – Heslacher Jul 6 '15 at 20:43
• @Heslacher My intention was never to undercut your answer - but to provide more information and explanation as to how your answer works. Both answers have valid merit, which I believe is the purpose of Code Review. (To generate multiple opinions.) – Der Kommissar Jul 6 '15 at 22:04
• Also note that BitConverter uses the endianness of the system, so you need additional configuration to use it in a deterministic fashion. – dfhwze Jul 9 '19 at 17:59
• You only need to get the bytes if line=='F' so you should move it inside the if block.

• If you only need to get these 3 Int16 values, IMHO using Array.Copy, Array.Reverse is a little bit too much.

while(SerialPort.BytesToRead > 50){

if (line=='F'){
byte[] encoded = enc.GetBytes(line);

short value1 = BitConverter.ToInt16(new byte[] {encoded , encoded }, 0);
short value2 = BitConverter.ToInt16(new byte[] {encoded , encoded }, 0);
short value3 = BitConverter.ToInt16(new byte[] {encoded , encoded }, 0);
}
}

• naming variables value1, value2 and value3 screams for new meaningful names and for a List<T> or any other collection. In this way the shown conversation could be done using a loop.

• comments like // read line and turn string in byte array does not add any value to the code, because the comment is describing what is done instead of why something is done.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8456372,"math_prob":0.9046965,"size":4357,"snap":"2020-34-2020-40","text_gpt3_token_len":1192,"char_repetition_ratio":0.1405927,"word_repetition_ratio":0.08792847,"special_character_ratio":0.3093872,"punctuation_ratio":0.18119267,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9553259,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-09T03:18:08Z\",\"WARC-Record-ID\":\"<urn:uuid:0f2f4789-9ba5-486a-9333-c02d97d91b64>\",\"Content-Length\":\"165804\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5e2434a0-761a-4789-a127-fb197045fff8>\",\"WARC-Concurrent-To\":\"<urn:uuid:5d368555-74a5-4a41-8835-4ea4631dde87>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/95983/read-binary-serial-data-and-parse-integers/96009\",\"WARC-Payload-Digest\":\"sha1:GY3DDIF22EQ6MVADB6OE6UD627UL43PB\",\"WARC-Block-Digest\":\"sha1:2D7SNT464RIPPUQFA3YFRDHXILNH7Z2D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738380.22_warc_CC-MAIN-20200809013812-20200809043812-00036.warc.gz\"}"}