Delphi - Convert byte array to string

Viewed 94134

How do I convert a byte array to a string (base 256) in Delphi?

5 Answers

I think there is another nice way to convert byte arrays in strings - an Indy function called BytesToString contained in IdGlobal. It also allows you to specify StartIndex, Length and TEncoding for your string. I've used it several times and I find it very useful.

    function bintostr_r(const bin: array of byte): string;
    var i,j:integer;
        res:string ;
    begin
    res:='';
        for i:=0 to length(bin)-1 do
        begin
           for j:=1 to 8 do
           res:=Inttostr( ((bin[i] shr (j - 1)) and ((1 shl 1) - 1)) ) +res  ;
        end;
        result:=res;

    end;



procedure TForm1.FormCreate(Sender: TObject);
var OrigStat: array [1..6] of byte;  
  res:integer;
begin

  OrigStat[1]:=253; // 11111101
  OrigStat[2]:=252;
  OrigStat[3]:=251;
  OrigStat[4]:=250;
  OrigStat[5]:=249;
  OrigStat[6]:=248;
  Edit9.text:=bintostr_r(OrigStat);
end;


result => 111110001111100111111010111110111111110011111101

Related