这是原来在BC中的过程,
String __fastcall TCall::HexToStr(int n)
{
String str;
    char ch[10];
wsprintf(ch, "%02x", (BYTE)n);
    str = ch;
    return str;
}我转换成DELPHI中使用时
var
  ch:array[1..10] of char;
begin
   wsprintf(@ch,'%02x',byte(n);这时,会提示说 byte(n)这个参数是多余的,有什么办法吗?
上面那个过程应该是把 16进制转成string输出的
那我是否可以使用 inttohex函数来实现?谢谢

解决方案 »

  1.   

    真是 奇怪...原始的API 是这样声明的
    int wsprintf(    LPTSTR lpOut, // pointer to buffer for output 
        LPCTSTR lpFmt,  // pointer to format-control string 
        ... // optional arguments
       );
    而delphi自己的声明呢:
    function wsprintf(Output: PChar; Format: PChar): Integer; stdcall;
    。 
    这个函数很强悍的
      

  2.   

    var
      name : string;  //人的姓名
      age : integer;  //人的年龄
      str: PChar;
    begin
      name :='张三';
      age :=23;
      GetMem(str, 255);
      wsprintf(str, PChar(Format('%s的年龄是%d岁', [name, age])));
      messagebox(0,str,'标题',0);
      FreeMem(str);
    end;
      

  3.   

    Note  Unlike other Windows functions, wsprintf uses the C calling convention (_cdecl), rather than the Pascal calling convention. As a result, it is the responsibility of the calling process to pop arguments off the stack, and arguments are pushed on the stack from right to left. In C-language modules, the C compiler performs this task.  
      

  4.   

    哈哈
    我也是最近才明白的,不如直接用Format
    根本没有实现可变参数!采用了这个变通的办法
    还要感谢月亮:)
      

  5.   

    hongqi162(失踪的月亮) ( ) 信誉:105 按你的试一下,
      

  6.   

    // 改用wvsprintf ..........program  ScrnSize;uses
      Windows;  // 格式化输出函数        {标题}   {输出格式}     {输出变量列表}
    procedure MessageBoxPrintf(Caption, Format: PChar; const Args: array of const);
    var
      OutBuff: array[0..100] of Char; // 最终输出文字
      OutList: array of LongWord;     // 输出变量列表
      J: Integer;
    begin
      SetLength(OutList, 0);
      for J := Low(Args) to High(Args) do // Args -> OutList
      begin
        case Args[J].VType of  // 这里仅处理了两种类型
          vtInteger   : begin
                          SetLength(OutList, Length(OutList)+1);
                          OutList[High(OutList)] := Args[J].VInteger;
                        end;
          vtString,
          vtAnsiString: begin
                          SetLength(OutList, Length(OutList)+1);
                          OutList[High(OutList)] := LongWord(Args[J].VString);
                        end;
        end;
      end;
      if Length(OutList) = Length(Args) then // 列表成员均合法
      begin
        wvsprintf(OutBuff, Format, @OutList[0]);  // 转换
        MessageBox(0, OutBuff, Caption, 0);       // 输出
      end;
    end;var
      cxScreen, cyScreen: Integer;begin
      cxScreen := GetSystemMetrics(SM_CXSCREEN); // 屏幕宽度
      cyScreen := GetSystemMetrics(SM_CYSCREEN); // 屏幕高度
      MessageBoxPrintf('ScrnSize',               
                       'The screen is %i pixels wide by %i pixels high.',
                       [cxScreen, cyScreen]);
    end.
      

  7.   

    OK了,
    直接使用 format与wsprintf没有什么区别的吧?  n:=strtoint(edit1.Text );
      s:=format('%x',[n]);但 上面所与的 ‘%2X’与 现在的 '%x'有什么区别吗?我运行得出的结果是一样的