比如下面的代码
var
  buf:array [0..4095] of AnsiChar;
  stmp:string;
  i:integer;
begin
  buf := '已使用';  //正常
  
  //但是如果加个变量就不正常了。 如:
  i:=5;
  buf := '已使用个数:'+ inttostr(i);   //报错,  inttostr 是转换为string类型,不能直接加在char数组后面,如果需要这样赋值,该怎么办呢?
end;

解决方案 »

  1.   

    LZ 啊,不要把编译器的优化当成理所当然啊,用move吧
    str:='已使用个数:'+ inttostr(i);
    move(str[1],buf[0],length(str))
      

  2.   

    Delphi中木有sprintf函数,那是个好东西啊。
    可以先Format一下,再PChar转换。
      

  3.   

    Delphi中有FormatBuf,跟sprintf几乎是一样的:
    function FormatBuf(var Buffer; BufLen: Cardinal; const Format;
      FmtLen: Cardinal; const Args: array of const): Cardinal; overload;
    function FormatBuf(var Buffer; BufLen: Cardinal; const Format;
      FmtLen: Cardinal; const Args: array of const;
      const FormatSettings: TFormatSettings): Cardinal; overload;另外还有windows api:
    function wsprintf(Output: PChar; Format: PChar): Integer; stdcall;
      

  4.   

    var
      buf:array [0..4095] of AnsiChar;
      stmp:string;
      i:integer;
      str:AnsiString;
    begin
      buf := '已使用'; //正常
      i:=5;
      str := '已使用个数:'+ inttostr(i); 
      strcopy(buf,PAnsiChar(str));
      

  5.   

    用StrPCopy,代码如下:
    var
      buf:array [0..4095] of AnsiChar;
      stmp:string;
      i:integer;
      str:AnsiString;
    begin
      buf := '已使用'; //正常
      i:=5;
      str := '已使用个数:'+ inttostr(i);  
      StrPCopy(buf, str);
    end
      

  6.   

    如果要用string类型中转一下我知道怎么做,我就是想知道没有没可以直接将变量赋值到 char类型的方法。