0

Possible Duplicate:
Convert Array of ShortInt to String, Delphi

I would like to turn a file into a string, this string should contain the numbers corresponding to the file in bytes.

I did in a way but it was very slow, using an array of FOR and ShortInt ...

Example:

have any file on my computer, my goal would be to transform it into Bytes, Bytes that these should be between -127 .. 128, it would have with something like this:

A[0] = 120
A[1] = -35
A[2] = 40

Ate here OK, but I need it in a concatenated string and a ',' between them, thus:

'120,-35,40'

I did it with a 'FOR', but it was very slow, if you have another alternative.

5
  • Is related to the same thing, with different questions, that I'm looking for a solution to convert a file to byte and byte to this String. In another link, I'm trying to transform an Array in Bytes. Is on the same subject, but the questions are different. é sobre o mesmo assunto, mas as perguntas sao diferentes. Commented Nov 5, 2012 at 17:04
  • Apologies, I'm in need of an algorithm that reads a file and turn it into any Bytes, Bytes but these should be -127 to 127 and transforms them into STRING because he must go to a Web Service, and so he accepts a string. Ate got faser, most became very slow because used a FOR passing byte by byte converting them into Strings. Commented Nov 5, 2012 at 17:12
  • technically speaking, the Byte datatype is not in the range -127..128, because a byte is a unsigned integer (0..255). The ShortInt range is -128..127 (not exactly what you asked, tough), and it size is one byte, but is not the usual way to see a file, which in fact is using the hexadecimal representation of the Byte array, something like '78 DD 28' and not '120 -35 40' Commented Nov 6, 2012 at 1:33
  • And where's your other question? I saw an answer from RRUZ today and now I can't find your question... are you deleting your previous questions? why? Commented Nov 6, 2012 at 1:36
  • Not erased the previous questions, he refers to this question stackoverflow.com/questions/13233776/… Commented Nov 6, 2012 at 10:25

2 Answers 2

3

This question is rather similar to your previous question, but with the added complexity of reading the array from a file.

I'd probably write it like this:

function ConvertFileToCommaDelimitedArray(const FileName: string): string;
var
  i, BytesLeft, BytesRead: Integer;
  Buffer: array [0..4096-1] of Shortint;
  Stream: TFileStream;
  sb: TStringBuilder;
begin
  sb := TStringBuilder.Create;
  try
    Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
    try
      BytesLeft := Stream.Size;
      while BytesLeft>0 do
      begin
        BytesRead := Min(SizeOf(Buffer), BytesLeft)
        Stream.ReadBuffer(Buffer, BytesRead);
        dec(BytesLeft, BytesRead);
        for i := 0 to BytesRead-1 do
        begin
          sb.Append(IntToStr(Buffer[i]));
          sb.Append(',');
        end;
      end;
    finally
      Stream.Free;
    end;
    if sb.Length>0 then
      sb.Length := sb.Length-1;//remove trailing comma
    Result := sb.ToString;
  finally
    sb.Free;
  end;
end;
Sign up to request clarification or add additional context in comments.

1 Comment

@Jose Does this answer your question?
2

You can load a file into an array of one-byte signed integral types (also known as ShortInt) like this:

type
  TShortIntArray = array of TShortInt;

function LoadFileAsShortInt(const name: TFileName): TShortIntArray;
var
  f: TFileStream;
begin
  f := TFileStream.Create(name, fmOpenRead or fmShareDenyWrite);
  try
    SetLength(Result, f.Size);
    f.ReadBuffer(Result[0], f.Size);
  finally
    f.Free;
  end;
end;

If you want the file's contents as a string, then you should skip the array and load the file directly into a string:

function FileAsString(const name: TFileName): AnsiString;
var
  s: TStringStream;
begin
  s := TStringStream.Create;
  try
    s.LoadFromFile(name);
    Result := s.DataString;
  finally
    s.Free;
  end;
end;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.