29

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

seveleven
  • 697
  • 2
  • 13
  • 21

5 Answers5

47

Use the built-in SetString command. It sets the string to the required length and copies the bytes. There's no need for the array to be null-terminated. In fact, if the array has zero--valued bytes in it, they'll correctly appear within the string; they won't terminate the string.

SetString(AnsiStr, PAnsiChar(@ByteArray[0]), LengthOfByteArray);

If you have a UnicodeString, then you'll need to halve the length parameter since it measures characters, not bytes:

SetString(UnicodeStr, PWideChar(@ByteArray[0]), LengthOfByteArray div 2);

See also, Converting TMemoryStream to String in Delphi 2009.

Yennefer
  • 5,704
  • 7
  • 31
  • 44
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 14
    I got the length of byte array using Length(ByteArray). – seveleven Oct 08 '10 at 03:13
  • 1
    @Sergey, `AnsiStrintgT` and `UnicodeString` both have constructors that accept a `char*` and a length. So does `std::string`, which also has an `assign` method for the same purpose. – Rob Kennedy Mar 24 '15 at 11:56
  • Suggest less error prone `SetString(UnicodeStr, PWideChar(@ByteArray[0]), LengthOfByteArray div SizeOf(PWideChar^));` – Dominik Gebhart Oct 26 '17 at 10:35
10

I'm not sure what do you mean by Base256. If you want to get hex representation of data, use this:

function bintostr(const bin: array of byte): string;
const HexSymbols = '0123456789ABCDEF';
var i: integer;
begin
  SetLength(Result, 2*Length(bin));
  for i :=  0 to Length(bin)-1 do begin
    Result[1 + 2*i + 0] := HexSymbols[1 + bin[i] shr 4];
    Result[1 + 2*i + 1] := HexSymbols[1 + bin[i] and $0F];
  end;
end;

If you want to just render the data as a string (this doesn't change the content!), where for each byte of data you'd get a single ASCII symbol with that code, do

function bintoAscii(const bin: array of byte): AnsiString;
var i: integer;
begin
  SetLength(Result, Length(bin));
  for i := 0 to Length(bin)-1 do
    Result[1+i] := AnsiChar(bin[i]);
end;
himself
  • 4,806
  • 2
  • 27
  • 43
  • Base 256 string is an ASCII string, as you got it correctly it is Ansistring/String in Delphi. The function bintoAscii works perfectly. Just that need to declare var i. Thank u! – seveleven Oct 07 '10 at 13:17
  • 2
    If you just want to render the data you can just copy it to an AnsiString using Copy(Memory) – Remko Oct 07 '10 at 13:23
  • 3
    There are only 127 characters in the ASCII character set. Maybe you are referring to Windows-1252. – Andreas Rejbrand Oct 07 '10 at 17:38
6
var
   LString : string;
   LBytes : TArray<byte>;
begin    
   LBytes := TArray<byte>.Create($01, $02, $03);
   LString := TEncoding.ANSI.GetString(ABytes);
end;

Being GetString() the reverse operation of GetBytes().

drakorg
  • 404
  • 4
  • 14
2

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.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Boris D. Teoharov
  • 2,319
  • 4
  • 30
  • 49
-1

    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