Byte array manipulation

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Byte array manipulation

Wimpie Nortje
Hi all,

I have a lot of byte arrays which contains mostly text but also #0. I am
looking for functions to manipulate these arrays.

The stuff in strutils are mainly what I need but they operate on strings
which is not usable to me due to the #0. Are there any such functions in
FPC?

And while I am onto strings...does anyone know of a quick and easy way
to convert strings to a byte array. Much like aPchar := PChar(aString)?

Thanks
Wimpie
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Byte array manipulation

theo-6
AnsiString does not have problems with #0.

Test:

var aStr:AnsiString;
begin
aStr:='test-test-test';
aStr:=StringReplace(aStr,'-',#0,[rfReplaceAll]);
//if you ouput now, you'll only see 'test'...
aStr:=StringReplace(aStr,#0,'-',[rfReplaceAll]);
//...but the text is still in the String;
Edit1.text:=aStr;
end;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Byte array manipulation

Wimpie Nortje
theo wrote:

> AnsiString does not have problems with #0.
>
> Test:
>
> var aStr:AnsiString;
> begin
> aStr:='test-test-test';
> aStr:=StringReplace(aStr,'-',#0,[rfReplaceAll]);
> //if you ouput now, you'll only see 'test'...
> aStr:=StringReplace(aStr,#0,'-',[rfReplaceAll]);
> //...but the text is still in the String;
> Edit1.text:=aStr;
> end;
Thanks. I tested it and it works.

The reference manual is a bit confusing though:
"Ansistrings are strings that have no length limit. They are reference
counted and null terminated."

"i.e. a variable of type PChar is a pointer that points to an array of
type Char, which is ended by a null-character (#0). "

The above quotes does not agree with the code above, which makes
discovering this info a bit difficult.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Byte array manipulation

Andrew Brunner
In reply to this post by Wimpie Nortje
Hi there,

I have a ByteArray unit that's pretty extensive.

function toString(Var Item:TByteArray):string
var
  iLen:Integer;
begin
  iLen:=System.Length(Item);
  SetLength(Result,iLen);
  if iLen>0 then
    System.Move(Item[0],Result[1],iLen);
end;

On Sun, Aug 16, 2009 at 12:35 PM, Wimpie Nortje<[hidden email]> wrote:

> Hi all,
>
> I have a lot of byte arrays which contains mostly text but also #0. I am
> looking for functions to manipulate these arrays.
>
> The stuff in strutils are mainly what I need but they operate on strings
> which is not usable to me due to the #0. Are there any such functions in
> FPC?
>
> And while I am onto strings...does anyone know of a quick and easy way to
> convert strings to a byte array. Much like aPchar := PChar(aString)?
>
> Thanks
> Wimpie
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Byte array manipulation

Wimpie Nortje
Andrew Brunner wrote:

> Hi there,
>
> I have a ByteArray unit that's pretty extensive.
>
> function toString(Var Item:TByteArray):string
> var
>   iLen:Integer;
> begin
>   iLen:=System.Length(Item);
>   SetLength(Result,iLen);
>   if iLen>0 then
>     System.Move(Item[0],Result[1],iLen);
> end;
Thanks, this will be a useful function.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal