Reputation: 131
I am trying to write this into a byte array. I am not sure how to handle the text part.
Example : Setting an alphanumeric variable named OF with value TEST:
[0x02][0x00][0x35][0x37][0xFF][0x00]OF=TEST[0x00][0x03]
I know how to write the given hex in the above example, however, when I get to the OF=TEST, I need to know how to fit that into the byte array.
byte[] byteData = {0x02, 0x00, 0x35, 0x37, 0xFF, What do I do here?, 0x00, 0x03};
Upvotes: 0
Views: 516
Reputation: 24430
byte[] preByteData = {0x02, 0x00, 0x35, 0x37, 0xFF};
byte[] postByteData = {0x00, 0x03};
//using System.Linq;
byte[] byteData = preByteData.Concat(System.Text.Encoding.UTF8.GetBytes("OF=TEST").Concat(postByteData)).ToArray();
Upvotes: 1
Reputation: 74277
Something like this will do you:
byte[] octets ;
Encoding someEncoding = new UTF8Encoding(false) ;
using( MemoryStream aMemoryStream = new MemoryStream(8192) ) // let's start with 8k
using ( BinaryWriter writer = new BinaryWriter( aMemoryStream , someEncoding ) ) // wrap that puppy in a binary writer
{
byte[] prefix = { 0x02 , 0x00 , 0x35 , 0x37 , 0xFF , } ;
byte[] suffix = { 0x00 , 0x03 , } ;
writer.Write( prefix ) ;
writer.Write( "OF=TEST" );
writer.Write( suffix ) ;
octets = aMemoryStream.ToArray() ;
}
foreach ( byte octet in octets )
{
Console.WriteLine( "0x{0:X2}" , octet ) ;
}
Upvotes: 1