Autor Beitrag
dinazavric
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75



BeitragVerfasst: Mi 15.06.11 10:19 
Hallo,

ich möchte ein Byte-Array in mehrere Teile á 50 Byte aufteilen. Ich habe es damit versucht:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
byte[][] DataParts = new byte[50][];
int i, j = 0;

for (i = 0; i < (Data.Length - 1); i++)
{
  if ((i > 0) && (i%50 == 0)) j++; //50 bytes vorbei
  DataParts[i - j*50][j] = Data[i];
}


Leider funktioneiert es nicht... Was mache ich falsch?
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 15.06.11 10:40 
Zitat:
Was mache ich falsch


Du sagst nicht was passiert und was du erwartet hättest. So kann man wieder nur raten.

Ich vermute mal das data ein 1-dimensionales Byte Array ist und du ein 2-dimensionales daraus machen möchtest? Dann hast du die Dimensionen verwechselt(Du weist nicht das du 50 Teile hast sondern nur das jedes Teil 50 lang ist) und die 2-te Dimension nicht initialisiert.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
byte[][] DataParts = new byte[(Data.Length / 50)+1][];

for (int i = 0; i < (Data.Length); i++)
{
    if (i % 50 == 0)
        DataParts[i / 50] = new byte[Math.Min(Data.Length-i, 50)];
    DataParts[i / 50][i % 50] = Data[i];
}


Achtung. Code nur durchdacht aber nicht laufen gelassen.
dinazavric Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 75



BeitragVerfasst: Mi 15.06.11 10:46 
user profile iconRalf Jansen hat folgendes geschrieben Zum zitierten Posting springen:
Ich vermute mal das data ein 1-dimensionales Byte Array ist und du ein 2-dimensionales daraus machen möchtest? Dann hast du die Dimensionen verwechselt(Du weist nicht das du 50 Teile hast sondern nur das jedes Teil 50 lang ist) und die 2-te Dimension nicht initialisiert.


Genau so ist es und danke, es hat funktioniert :-)
pdelvo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 55
Erhaltene Danke: 11



BeitragVerfasst: Mo 08.08.11 01:53 
Hier mal meine Lösung:


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
        public static T[][] Split<T>(T[] source, int parts)
        {
            bool div = source.Length % parts == 0;

            T[][] dataParts = new T[(source.Length / parts) + (div ? 0 : 1)][];

            for (int i = 0; i < dataParts.GetLength(0); i++)
            {
                T[] temp;
                temp = dataParts[i] = new T[(i == dataParts.GetLength(0) - 1) && !div ? (source.Length % parts) : parts];
                Array.Copy(source, i * parts, temp, 0, temp.Length);
            }

            return dataParts;
        }