(11-27-2019, 10:29 AM)Madammar Wrote: i tried to keep the original bytes after cutting it out
but its always get corrupted what i am doing wrong ?
Well, for starters, you are not calling BytesToString() correctly. The 3rd parameter is the number of bytes to copy, not a reference to a specific byte. It should be more like this instead:
Code:
straudio := BytesToString(AData, 6, Length(AData)-6);
But why are you copying the binary data to a string at all? Converting bytes to a string requires a character encoding, which you are not specifying, so Indy's default encoding will be used, which is US-ASCII by default. So you are still going to get corrupted data for bytes > $7F.
If you are going to use a string, at least use Indy's 8-bit encoding to avoid data loss:
Code:
straudio := BytesToString(AData, 6, Length(AData)-6, IndyTextEncoding_8Bit);
// or: straudio := BytesToStringRaw(AData, 6, Length(AData)-6);
deData := ToBytes(straudio, IndyTextEncoding_8Bit);
But really, just get rid of the string altogether, you don't need it at all:
Code:
procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
deData : TIdBytes;
begin
dedata := Copy(AData, 6, Length(AData)-6);
// use deData as needed...
end;
Or:
Code:
procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
deData : TIdBytes;
begin
deData := ToBytes(AData, Length(AData)-6, 6);
// use deData as needed...
end;
Or:
Code:
procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
deData : TIdBytes;
begin
SetLength(deData, Length(AData)-6);
CopyTIdBytes(AData, 6, deData, 0, Length(deData));
// use deData as needed...
end;
Or, simply use a pointer to the data, don't even make a copy:
Code:
procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread;
const AData: TIdBytes; ABinding: TIdSocketHandle);
var
AudioDataSize: Integer;
deData: PByte;
begin
AudioDataSize := Length(AData)-6;
deData := @AData[6];
// use deData up to AudioDataSize bytes as needed...
end;