Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2009-08

Byte array manipulation

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

theo
to Wimpie Nortje

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;

Reply by email

Wimpie Nortje
to theo
theo wrote 13 lines
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.

Reply by email

Andrew Brunner
to Wimpie Nortje: I have a lot of byte arrays which contains mostly text but also #0. I am looking for…

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;
Wimpie Nortje wrote 18 lines
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  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Wimpie Nortje
to Andrew Brunner
Andrew Brunner wrote 13 lines
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.

Reply by email