Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can i add text to rawtobytedata ?
#8
(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;

Reply


Messages In This Thread
can i add text to rawtobytedata ? - by Madammar - 09-19-2019, 09:13 AM
RE: can i add text to rawtobytedata ? - by rlebeau - 11-27-2019, 06:39 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)