can i add text to rawtobytedata ? - Printable Version +- Atozed Forums (https://www.atozed.com/forums) +-- Forum: Indy (https://www.atozed.com/forums/forum-8.html) +--- Forum: Indy General Discussion (https://www.atozed.com/forums/forum-9.html) +--- Thread: can i add text to rawtobytedata ? (/thread-1252.html) |
can i add text to rawtobytedata ? - Madammar - 09-19-2019 i have the following code Code: procedure Tform1.rData(Sender: TObject; const Buffer: Pointer; i want to add a text identification to each audio sent and then remove that text when its received like following as example Code: if (Buffer <> nil) and (BufferSize > 0) And (udpreciver.Active) then how to do this in correct way RE: can i add text to rawtobytedata ? - rlebeau - 09-19-2019 (09-19-2019, 09:13 AM)Madammar Wrote: i want to add a text identification to each audio sent and then remove that text when its received There are many different ways you can handle this. Code: // Delphi XE7 or later only ... Code: var Bytes: TidBytes; Code: var Bytes: TidBytes; Code: var Bytes: TIdBytes; RE: can i add text to rawtobytedata ? - Madammar - 09-21-2019 in receiving part can i delete the Audio1 Text and keep the actual byets ? RE: can i add text to rawtobytedata ? - rlebeau - 09-24-2019 (09-21-2019, 04:41 PM)Madammar Wrote: in receiving part can i delete the Audio1 Text and keep the actual byets ? You could, by simply using System.Delete() (XE7+ only) or Indy's RemoveBytes(). Or, you could Copy()/CopyTIdBytes() the wanted bytes to another array. But really, there is usually no need to do any of this. If the 1st 6 bytes are 'Audio1', just ignore those bytes and use/save the remaining bytes as needed. What is so hard about that? RE: can i add text to rawtobytedata ? - Madammar - 09-30-2019 sorry for my bad english in my replys i wasn't mean to delete the text i was just asking about splitting this buffer to differentiate between each audio sent sense i am using Audio1 as identification or more likely a header thats why i wanted to know a way to split this buffer to check if this audio have a text of audio1 or not which came to my mind i have to split out the Audio1 to a string variable and do the checking RE: can i add text to rawtobytedata ? - rlebeau - 09-30-2019 (09-30-2019, 02:48 PM)Madammar Wrote: which came to my mind i have to split out the Audio1 to a string variable and do the checking Indy has BytesToString() and BytesToStringRaw() functions, which take an optional index and length as input, eg: Code: if BytesToString(bytes, 0, 6) = 'Audio1' then Or, you could simply use the RTL's CompareMem() function, eg: Code: if (Length(bytes) >= 6) and CompareMem(PByte(bytes), PAnsiChar('Audio1'), 6) = 0 then RE: can i add text to rawtobytedata ? - Madammar - 11-27-2019 Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread; i tried to keep the original bytes after cutting it out but its always get corrupted what i am doing wrong ? RE: can i add text to rawtobytedata ? - rlebeau - 11-27-2019 (11-27-2019, 10:29 AM)Madammar Wrote: i tried to keep the original bytes after cutting it out 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); But really, just get rid of the string altogether, you don't need it at all: Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread; Or: Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread; Or: Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread; Or, simply use a pointer to the data, don't even make a copy: Code: procedure TForm1.udpreciverUDPRead(AThread: TIdUDPListenerThread; |