Jump to content

Programming C#, concatenate 2 byte arrays


Recommended Posts

Posted

Hi.

 

I am working on a lockon project. Therefore i hope sum1 could help me with something which ould be simple in C#.

 

I dont know how to concatenate two binary arrays, such as one would easily do with a string.

 

Look at my code.

		public byte[] GetXMLtoMIS(XmlDocument xDocToConvert)
	{
		string strLockonMissionHeader = "";
		byte[] bytMissionHeader = null;

		try
		{
			StreamReader srIn1 = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @".\" + ConfigurationSettings.AppSettings["LockonMissionHeader"]);
			BinaryReader brInput1 = new BinaryReader(srIn1.BaseStream);

			if(brInput1 != null)
			{
				bytMissionHeader = brInput1.ReadBytes(Convert.ToInt32(brInput1.BaseStream.Length));
				bytMissionHeader = bytMissionHeader + System.Text.Encoding.UTF8.GetBytes(xDocToConvert.OuterXml.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", ""));
			}
		}
		catch(Exception ex)
		{
			throw new Exception(ex.Message + "; " + ex.StackTrace);
		}

		return bytMissionHeader;
	}

 

 

This is the piece of code that annoys me:

bytMissionHeader = bytMissionHeader + System.Text.Encoding.UTF8.GetBytes("bla die bl bla"));

 

It results in error "C:\michiel\Projecten\Ander\Lockon editor\pappavis.lockon.mapeditorGUI\editor\clsMissionFile.cs(583): Operator '+' cannot be applied to operands of type 'byte[]' and 'byte[]'".

 

ANy1 with an idea?

 

TIA!!

met vriendelijke groet,

Михель

 

"умный, спортсмен, комсомолетс"

 

[sIGPIC]159th_pappavis.jpg[/sIGPIC]

 

[TABLE]SPECS: i9-9900K 32gigs RAM, Geforce 2070RTX, Creative XFi Fata1ity, TIR5, Valve Index & HP Reverb, HOTAS Warthog, Logitech G933 Headset, 10Tb storage.[/TABLE]

Posted

Hi

 

Problem is that an array, in your case 'bytMissionHeader' has a fixed size, as all arrays. You need to know how many bytes you are going to store, when you create the array.

 

int size = CalcNeededSize();

byte[] bytMissionHeader' = new byte;

 

Maybe someone else can help you further, I have not done much C#.

 

Is there some kind of vector or list class in C# that can grow as you need it?

 

Good luck!

Posted

I used J# when it first came out, and it's been awhile... and I never touched C#.

 

I think you need to make your own concat function/member.

 

You will need to be able to access each byte and set them. Not sure if the [] will let you do that.

 

For example,

public void concatByteArrays( byte[] byteArray1, byte[] byteArray2, int array1Length, int array2Length )
{
 int totalLength = array1Length + array2Length;
 byte[] newByteArray = new byte[ totalLength ];
 int i = 0;

 while ( i < array1Length )
   newByteArray[ i ] = byteArray1[ i++ ];

 while ( i < totalLength )
 {
   newByteArray[ i ] = byteArray2[ i - array1Length ];
   i++;
 }

 byteArray1 = newByteArray; // can assignment be made for arrays?  
                                        // if not, return newByteArray
}

 

Pappavis, you use MSDN? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfsingledimensionalarrays.asp

 

[EDIT1] You can probably get the length of the new byte array from the streamreader.

sig36aa.jpg
Posted

Thanx, this worked perfect :).

met vriendelijke groet,

Михель

 

"умный, спортсмен, комсомолетс"

 

[sIGPIC]159th_pappavis.jpg[/sIGPIC]

 

[TABLE]SPECS: i9-9900K 32gigs RAM, Geforce 2070RTX, Creative XFi Fata1ity, TIR5, Valve Index & HP Reverb, HOTAS Warthog, Logitech G933 Headset, 10Tb storage.[/TABLE]

Posted

C# ArrayList

 

You can also use ArrayList (dynamic array):

 

       XmlDocument xDocToConvert;
       ArrayList ALMissionHeader = new ArrayList();

       StreamReader srIn1 = new StreamReader("C:\\test.xml");
       BinaryReader brInput1 = new BinaryReader(srIn1.BaseStream);

       ALMissionHeader.AddRange( brInput1.ReadBytes(Convert.ToInt32(brInput1.BaseStream.Length)) );
       ALMissionHeader.AddRange( System.Text.Encoding.UTF8.GetBytes(xDocToConvert.OuterXml.Replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "")) );

(not tested, but it compiles)

 

Getting it back from ArrayList to byte[] is a bit trickier though.

 

Cheers.

LP

 

modules:

F5-E / A4-E / A-10A / AJS-37 / SA-342 / UH-1H / Ka-50 / Mi-8 / CA

 

would buy:

OH-58 /AH-64A / AH-1 / Sepecat Jaguar / F-111

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...