Autor Beitrag
jeng
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 30



BeitragVerfasst: Mo 24.08.15 13:08 
Hallo,

Ich habe folgendes Problem.

Ich möchte gerne von einem String (Länge 131bytes inclusieve #$05#$01) in einzelne arrays von 8 byte länge aufteilen.
so sollte es aussehen MyStrArray[0]: 00,11,22,33,44,55,66,77

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
begin
    scan := #$05#$01;
    Sock.SendString(scan);
    X := Sock.RecvPacket(500);
    Position := (length(X) -2div 8;
    Setlength(MyStrArray, Position);
    count := -1;
    

    for iFor := 0 to Length(X) do
      begin
      n:=0;
      repeat
        count := count +1;
        MyStrArray[count] := StringToHex(X[n]);
        inc(n,8)//adds 8 to n
      until n>Length(X);
    end;
Delphi-Laie
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1600
Erhaltene Danke: 232


Delphi 2 - RAD-Studio 10.1 Berlin
BeitragVerfasst: Mo 24.08.15 22:13 
user profile iconjeng hat folgendes geschrieben Zum zitierten Posting springen:
Ich habe folgendes Problem.


Welches?
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10181
Erhaltene Danke: 1254

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Di 25.08.15 12:55 
Moin!

user profile iconjeng hat folgendes geschrieben Zum zitierten Posting springen:
Ich möchte gerne von einem String (Länge 131bytes inclusieve #$05#$01) in einzelne arrays von 8 byte länge aufteilen.
Da du nicht genau darauf eingehst, welche Delphi-Version, welche Variablen-Deklaration und welche Komponenten du verwendest, gibt es hier etwas Unklarheit, wie man da ran gehen soll.

Sofern du Delphi >= D2k9 verwendest, dann sind generische Strings Unicode, und hier ist ein Zeichen nicht gleich einem Byte (diese Strings sind also nicht binary-safe). :idea: Weiterhin hast du bei den Indy-Komponenten >= 10 bei Strings ein Encoding zu beachten, und das dürfte im Default UTF8 sein (und nicht ANSI), hier kannst du also auch keine binary-safe-Strings verwenden (tust du´s trotzdem, kann es "Verfälschungen" der übertragenen Daten geben). :shock:

Fazit: Sofern du das tust, was ich vermute, dann darfst du gar keine Strings verwenden, sondern nur Byte-Arrays (weil du Binärdaten übertragen willst). :les: :think:

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Blup
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 173
Erhaltene Danke: 43



BeitragVerfasst: Di 25.08.15 17:48 
Könnte etwa so funktionieren:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
uses
  Types;

function BufferToHexDump(const ABuffer: AnsiString): TStringDynArray;
var
  i1: Integer; // Index im Array
  i2: Integer; // Hexcodes im String
  n: Integer;  // Index im Buffer
begin
  {alten Inhalt von Result löschen}
  SetLength(Result, 0);
  {1..8 Zeichen = 1.Zeile, 9..15 = 2.Zeile usw.}
  SetLength(Result, (Length(ABuffer) + 7div 8);

  n := 0;
  for i1 := 0 to High(Result) do
  begin
    for i2 := 0 to 7 do
    begin
      Inc(n);
      if n <= Length(ABuffer) then
      begin
        if i2 <> 0 then
          Result[i1] := Result[i1] + ',';
      
        Result[i1] := Result[i1] + IntToHex(Ord(ABuffer[n]), 2);
      end;
    end;
  end;
end;

const
  scan: AnsiString = #$05#$01;
var
  X: AnsiString;
  MyStrArray: TStringDynArray;
begin
  Sock.SendString(scan);
  X := Sock.RecvPacket(500);
  MyStrArray := BufferToHexDump(X);
end;

Für diesen Beitrag haben gedankt: jeng