Umwandlung von Byte[] Array in String dauert zu lange ;-(

OliWan

Mitglied
Hi Coders!

Ich hab da ein blödes Zeitrpoblem beim Bearbeiten eines ByteArray.

Ich krieg als Objekt ein Byte Array übergeben. In diese Sind manche Inhalte, die
ich gerne austauschen möchte. z.B. 00 45 in 00 00. Da ich nicht weis, in welchem
Arrayindex diese Inhalte liegen, gehe ich Augenblick so vor:

Code:
Byte[] meinarray=(Byte[]) daten;
String hexString="";
foreach (byte b in meinarray){
   hexString += String.Format("{0:X2}",b);
}
hexString.Replace("0045","0000");

int numBytesx = hexString.Length / 2;
byte[] arrBytex= new byte[numBytesx];

for (int z=0; z< numBytesx; ++z){
   arrBytex[z] = byte.Parse(hexString.Substring(z*2,2), System.Globalization.NumberStyles.HexNumber);
}

DUMMERWEISE aber, dauert das Umwandeln vom byte[] in den formatierten String ne
Ewigkeit. Wenn ich nen 60 Byte großes Array kriege, läuft das bei mir 3 Minuten ;-(

Kann mir jemand von euch ne Idee geben, wie ich das besser machen kann?
Evtl. mit Serilization?
Kann man das Array irgendwie intelligent selbst manipulieren?

Hope for help and so far
may the source be with you
OliWan:rolleyes:
 
Hallo,

das Array erst in einen String zu wandeln, dann dort eine Replace-Funktion laufen zu lassen und am Schluss wieder alles in ein Array zurückzukonvertieren ist wirklich nicht sehr performant :)
Dabei ist letzte Schleife mit "byte.parse ..." der eigentliche Performancekiller. Spezielle Funktionen für sowas fallen mir nicht ein, aber die Umwandlung könnte man trotzdem innerhalb einer Schleife (ohne den Umweg der Stringkonvertierung) erledigen:
C++:
byte [] arrBytex = new byte[meinarray.Length];
                        
for( int i = 0; i < meinarray.Length; i++ )
{
    if( i > 0 && meinarray[i-1] == 0 && meinarray[i] == 0x45 )
    {
        arrBytex[i-1] = 0;
        arrBytex[i]   = 0;
    }
    else
    {
        arrBytex[i] = meinarray[i];
    }
}
Gruß
MCoder
 
Hi.

Ja natürlich geht das schneller. Diese ganze String-Umwandlung ist ja umsonst.. :)

Hier ein kleines Beispiel:

C#:
using System;
using System.Diagnostics;

public class ConvertTests
{
	public static void Main(string[] args)
	{

		byte[] data = new byte[700];

		for (byte i = 0; i < 60; i++)
			data[i] = i;

		data[60] = 0x00; // data[60] und data[61] sollten ersetzt werden
		data[61] = 0x45;
		data[62] = 0x45;

		for (int i = 63; i < 700; i++)
			data[i] = (byte) (i % 255);


		Stopwatch watch = new Stopwatch();
		watch.Start();

		byte[] replaced = ByteReplace(data, new byte[] { 0x00, 0x45 }, new byte[] { 0x00, 0x00 });

		watch.Stop();

		Console.WriteLine("Replace took {0} ms", watch.ElapsedMilliseconds);
		for (int i = 0; i < data.Length; i++)
		{
			if (data[i]!=replaced[i])
				Console.WriteLine("#{0:00}\t{1} -> {2}",
				i, data[i], replaced[i]);
		}

		Console.Read();
	}

	public static byte[] ByteReplace(byte[] data, byte[] pattern, byte[] replacement)
	{
		if (pattern.Length != replacement.Length)
			throw new ArgumentException("must have the same length as pattern", "replacement");

		byte[] replaced = new byte[data.Length];

		Array.Copy(data, replaced, data.Length);

		bool match;

		for (int i = 0; i <= data.Length - pattern.Length; i++)
		{
			match = true;

			for (int j = 0; j < pattern.Length; j++)
			{
				if (data[i + j] != pattern[j])
				{
					match = false;
					break;
				}
			}

			if (match)
			{
				for (int j = 0; j < replacement.Length; j++)
				{
					replaced[i + j] = replacement[j];
				}
				i += replacement.Length - 1;
			}
		}

		return replaced;
	}
}

lg
 
Zurück