Working with bytes in c#

Hi experts,

           I will be getting a data in bytes[] during runtime.
           The data is categorized like,
           First 4 bytes - Integer - VersionNo
           Second 4 bytes - Integer - PocketID
           Third 2 bytes - Integer - NoOfPackets
           Fourth - can be any size in bytes - Description
 
Now i have already declared the variables to store in the corresponding values.

        public int m_iICDVersion;
        public int m_iPacketID;
        public int m_iPacketCounter0;
        public int m_iNoOfBytes;
        public string m_strData;


I want a function to segregate the whole byte[] and store the respective values to the declared variables.
srk1982Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

philipjonathanCommented:
Try:
public int m_iICDVersion = BitConverter.ToInt32(bytes, 0);
public int m_iPacketID = BitConverter.ToInt32(bytes, 4);
public int m_iPacketCounter0;
public int m_iNoOfBytes = BitConverter.ToInt16(bytes, 8);
public string m_strData = BitConverter.ToString(bytes, 10);


Btw, be careful about the endianness of your byte[]. The above assume little-endian
0
GuitarRichCommented:
You can use the BitConverter class to do that - like this:
example code http://msdn.microsoft.com/en-us/library/bb384066.aspx
For the final string you will need to strip out the integer bytes from the array or create a new array that just contains the string data.


m_iICDVersion = BitConverter.ToInt32(bytes, 0);
m_iPacketID = BitConverter.ToInt32(bytes, 4);
m_iPacketCounter0 = BitConverter.ToInt16(bytes, 8);
m_iNoOfBytes = bytes.Length;
byte[]  stringBytes = // Create the string byte array here.
m_strData = BitConverter.ToString(stringBytes);

Open in new window

0
srk1982Author Commented:
hi GuitarRich,

Thanks for ur prompt response.

Since we are using only  startindex it takes the rest of the values also. i guess we have to use endindex too. Am i right ? and some other problems are also there..

m_iNoOfBytes  = bytes.Length;
The above one gets the total legnth of bytes. But i my sequence is
           First 4 bytes - Integer - VersionNo
           Second 4 bytes - Integer - PocketID
           Third 2 bytes - Integer - NoOfPackets
           Fourth - can be any size in bytes - Description

_____________________________________________________________
 byte[]  stringBytes = // Create the string byte array here.
m_strData = BitConverter.ToString(stringBytes);

The above one is getting the value and i dont understand this too.
_____________________________________________________________

Thanks..
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

GuitarRichCommented:
The ToInt32 and ToInt16 functions only require a start index as an Int32 is always 4 bytes and an Int16 is always 2 bytes. So you don't need an end index. Infact the ToInt32/16 functions only take a start index, there is no overload for an end.
So m_iICDVersion = BitConverter.ToInt32(bytes, 0); will take the elements from 0 to 3 and convert it to an int.
m_iPacketID = BitConverter.ToInt32(bytes, 4); will take the elements from 4 to 7 and convert it to an int.
 
For the string the ToString function also takes a start index - so just use
m_strData = BitConverter.ToString(stringBytes, 10);
Like philipjonathan suggested - that will take the array from the 10th index and convert the rest of the array to a string.
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
srk1982Author Commented:
Thanks.
0
srk1982Author Commented:
Hi GuitarRich,

          Sorry, i accepted it before testing the solution. I am half way thru now.

           I am getting the values 825307441, 842150450 etc.
           I need to convert these values to string again....

I used this:

            m_iICDVersion = BitConverter.ToInt32(bytes, 0);
            m_iPacketID = BitConverter.ToInt32(bytes, 4);
            m_iPacketCounter = BitConverter.ToInt32(bytes, 8);
            m_iNoOfPackets = BitConverter.ToInt16(bytes, 12);
            byte[] stringBytes = bytes;
            m_strData = BitConverter.ToString(stringBytes, 14);
0
GuitarRichCommented:
can you post me your byte array - I'll have a quick test.
0
srk1982Author Commented:
Hi GuitarRich,

Finally i have made it. I used the below code to do it.

 string strm_iICDVersion = System.Text.Encoding.ASCII.GetString(bytes, 0, 4);
            string strm_iPacketID = System.Text.Encoding.ASCII.GetString(bytes, 4, 4);
            string strm_iPacketCounter = System.Text.Encoding.ASCII.GetString(bytes, 8, 4);
            string strm_iNoOfPackets = System.Text.Encoding.ASCII.GetString(bytes, 12, 2);
            string strm_strData = System.Text.Encoding.ASCII.GetString(bytes, 14, bytes.Length - 14);

Thanks.
0
GuitarRichCommented:
ok cool - glad you got it working.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.