Click here to Skip to main content
15,923,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a byte array consisted of 266 bytes.

I want to break down the array at certain elements, convert those elements into relevant data- a couple of bools, uint, a string and a hex byte, upon conversion I want to actively update a set of global variables, then trigger some action when change on those global variables is detected.

How would I go about it, and what would be the most time efficient way to do it?

What I have tried:

Not looking for a ready example, just some pointers.
Updated 25-Feb-21 3:12am
[no name] 26-Feb-21 13:11pm    
You can use a ByteConverter to convert bytes.

ByteConverter Class (System.ComponentModel) | Microsoft Docs[^]

If your "globals" are properties (i.e. has a "setter"), the setter can raise an "event" when the property changes.

Properties | Microsoft Docs[^]

If bytes are contiguous, a BinaryReader may help.

And any "public static field or property" becomes a "global" ... no other hysterics required.
Member 14827175 27-Feb-21 7:12am    
Thanks, that was a helpful comment.

There's no such thing as a "global" variable in C#, or .NET really.

You're not going to "detect" anything happening to a variable. However, you can store what you consider "global" data in Properties in a static class. The property get/set code can do or call whatever you need to in response to setting a new value on those properties.

The rest of what to do isn't exactly clear so I don't know what to say about it.
Share this answer
Member 14827175 26-Feb-21 10:32am    
Thank you.
imho: using a bunch of bytes to store information that requires parsing to interpret is, generally, a bad idea.

While you can simulate "global" variables in C# using static Fields, or static Properties, or certain Types declared as Const (Constant), in static Classes; and, you can even (shudder, groan) define a static Class "outside" any NameSpace:

1) It is bad practice to do so: violates OOP; leads to hard to track down errors; makes unit testing difficult.

2) You can get notifications of changes to Property values by implementing INotifyPropertyChanged in your Classes: [^]

3) You can embed certain types of values in your C# Application Resources: [^]

4) You can make a non-static Class that will allow only one instance of it to be created: this is called a Singleton [^] ... there should be a compelling reason to use this structure.
Share this answer
Member 14827175 26-Feb-21 10:31am    
Thanks, your suggestions helped a lot.
How would I go about it, and what would be the most time efficient way to do it?

Assuming the byte array is fixed length fields, butchering a byte array is so basic operations that it is difficult to see a difference of efficiency between solutions.
Defining a structure may help in the operation.
Share this answer
Member 14827175 26-Feb-21 5:16am    
OK, thank you for your input.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900