Jump to content
  • Advertisement
Sign in to follow this  
MrRage

[.net] byte[] question

This topic is 4157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey there, I've got a buffer with byte[] data I'd like to process the data in this buffer in the most efficient way possible. Because the byte[] contains several numbers in ascii form I think it makes more sense to copy the bytes out into another byte array and convert that instead of converting the whole buffer into a string and using sub strings for the values. But I could be wrong so if I could get some advice or pointers on these things below that would be great, thanks. // byte[] copy function, I think there might be a .NET equivalent but I’m not sure
byte[] byteCopy(byte[] bytes, int begin, int end) {
  int size = end - begin;
  byte[] value = new byte[size];
  for (int i = 0; i < size; i++) {
    value = bytes[begin + i];
  }
  return value;
}
// converting from byte[] ascii data into an integer value.
byte[] buffer[] = new byte[1024]; 
...
byte[] number = byteCopy(buffer, 0, 2);
int station = Int32.Parse(ASCIIEncoding.UTF8.GetString(number));
try {
  station = Int32.Parse(ASCIIEncoding.UTF8.GetString(number));
} catch (Exception e) {
  pannel.UpdateLog("Invalid Data.");
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by MrRage
Hey there,
I've got a buffer with byte[] data I'd like to process the data in this buffer in the most efficient way possible. Because the byte[] contains several numbers in ascii form I think it makes more sense to copy the bytes out into another byte array and convert that instead of converting the whole buffer into a string and using sub strings for the values. But I could be wrong so if I could get some advice or pointers on these things below that would be great, thanks.

// byte[] copy function, I think there might be a .NET equivalent but I’m not sure

byte[] byteCopy(byte[] bytes, int begin, int end) {
int size = end - begin;
byte[] value = new byte[size];
for (int i = 0; i < size; i++) {
value = bytes[begin + i];
}
return value;
}



Use Array.Copy.

ie:


Array.Copy( source, beginsource, destination, begindestination, length );




Quote:

// converting from byte[] ascii data into an integer value.

byte[] buffer[] = new byte[1024];
...
byte[] number = byteCopy(buffer, 0, 2);
int station = Int32.Parse(ASCIIEncoding.UTF8.GetString(number));
try {
station = Int32.Parse(ASCIIEncoding.UTF8.GetString(number));
} catch (Exception e) {
pannel.UpdateLog("Invalid Data.");
}



Don't bother copying the array, by the way. Use the other GetString function, and use Int32.TryParse as well; ie:


string str = ASCIIEncoding.UTF8.GetString( buffer, start, length );
int number;
if( Int32.TryParse( str, out number )
{
// ok!
}
else
{
// error!
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!