Results 1 to 5 of 5

Thread: How to get a string from a buffer

  1. #1

    How to get a string from a buffer

    This is something i never remember..
    If i have a buffer (ie, a pointer and a size), how do i obtain a string?

    [pascal]function BufferToString(prtointer; size:cardinal):string;
    begin
    result:=
    end;[/pascal]
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  2. #2

    How to get a string from a buffer

    Code:
    function BufferToString&#40;Src&#58; Pointer; Size&#58; Integer&#41;&#58; AnsiString;
    begin
      SetLength&#40;Result, Size&#41;;
      Move&#40;Src^, Pointer&#40;Result&#41;^, Size&#41;;
    end;

  3. #3

    How to get a string from a buffer

    ah perfect i'll try to keep it in mind!
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  4. #4

    How to get a string from a buffer

    Quote Originally Posted by Setharian
    Code:
    function BufferToString&#40;Src&#58; Pointer; Size&#58; Integer&#41;&#58; AnsiString;
    begin
      SetLength&#40;Result, Size&#41;;
      Move&#40;Src^, Pointer&#40;Result&#41;^, Size&#41;;
    end;
    Looks a little fishy to me. This might compile and work but isn't it a bit c-ish. What type will a pointer have when dereferenced? Yes, I know Move works with untyped parameters but I still don't like it. I would rather write it like this:
    Code:
    function BufferToString&#40;Src&#58; Pointer; Size&#58; Integer&#41;&#58; AnsiString;
    begin
      SetLength&#40;Result, Size&#41;;
      Move&#40;PByteArray&#40;Src&#41;^&#91;0&#93;, Result&#91;1&#93;, Size&#41;;
    end;
    Wouldn't Pointer(Result)^ be incorrect too? (Pointer(Result[1])^)?

    Edit: Excuse me my nosy-ness. I've been programming C for 16 hours :shock:
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5

    How to get a string from a buffer

    It will compile down to the same code. Your code will just cause a bigger headache for the compiler.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •