请高人把下面的代码转为delphi的代码,急用啊!非常感谢啊!!!本人对C一点都不懂啊...
{
   int handle;
   if(!(RadioButton_EditWhite->Checked || RadioButton_EditBlack->Checked))
    return;
   String stra = MaskEdit_WriteID->Text;
   BYTE *pchara= stra.c_str();
   BYTE x=pchara[0]-'0';
   BYTE y =pchara[1]-'0';
   x=x*10 + y;
   if(x>42)
   {
       MaskEdit_WriteID->Text = "0000000000";
       return;
   }
   UINT nTmp = StrToInt64(MaskEdit_WriteID->Text);
   BYTE ByteTmp[10];
   int j;
   for(j=0; j<10; j++)
   {
      ByteTmp[j] = nTmp/GetMyByte(10, 9-j);
      nTmp = nTmp%GetMyByte(10, 9-j);
   }
   for(j=0; j<5; j++)
      ByteTmp[j] = (ByteTmp[j*2]<<4)|(ByteTmp[j*2+1]& 0x0f);
   handle = open(strWriteFileDir.c_str(), O_CREAT |O_APPEND|O_RDWR |O_BINARY , S_IWRITE|S_IREAD);
   if (handle == -1)
   {
      Label100->Caption = "Have Error!";
   }
   else
   {
      if(filelength(handle)==0L)
      {
         BYTE ch;
         if(RadioButton_EditWhite->Checked)
         {
            ch = 0xaa;
            write(handle, &ch, 1);
         }
         if(RadioButton_EditBlack->Checked)
         {
            ch = 0xbb;
            write(handle, &ch, 1);
         }
      }     Label100->Caption = "Is Writing...";
     if(write(handle, ByteTmp, 5)==-1)
     {
        if(errno==EACCES )
          Label100->Caption = "Permission denied";
        else if(errno==EBADF)
           Label100->Caption=" Bad file number";
        else
           Label100->Caption=" Write file error!" ;
      }
      else
      Label100->Caption = "Have Writed!";
      close(handle);
   }
}    

解决方案 »

  1.   

    x应该是把一个字符串格式的十六进制转为10进制后的值,GetMyByte应该是个自定义函数,strWriteFileDir都没有定义过,应该是全局的文件路径字符串,光有这段代码怕翻译不全吧,看懂功能了用Delphi写吧
      

  2.   

    var
      Handle,j:Integer;
      Stra:string;
      PChara:PChar;
      x,y,ch:Byte;
      nTmp:UINT;
      ByteTmp:array[0..9] of Byte;
    begin
          if(not(RadioButton_EditWhite.Checked or RadioButton_EditBlack.Checked)) 
             Exit; 
          Stra   :=   MaskEdit_WriteID.Text; 
          pchara :=   PChar(stra);
          x := Byte(pchara[0])-Byte('0'); 
          y   :=Byte(pchara[1])-Byte('0'); 
          x:=x*10   +   y; 
          if(x> 42) then
          begin
                  MaskEdit_WriteID.Text   :=   '0000000000';
                  Exit; 
          end; 
          nTmp  :=   StrToInt64(MaskEdit_WriteID.Text); 
           
          for j:=0 to  9  do
          begin
                ByteTmp[j]   :=   nTmp/GetMyByte(10,   9-j); 
                nTmp   :=   nTmp mod GetMyByte(10,   9-j); 
          end; 
          for j:=0    to 4 do
                ByteTmp[j]   :=   (ByteTmp[j*2]  shl 4) or(ByteTmp[j*2+1]and   $0f); 
          handle   :=   open(PChar(strWriteFileDir),   O_CREAT or O_APPEND or O_RDWR  or O_BINARY   ,   S_IWRITE  or S_IREAD); 
          if   (handle   =   -1)  then
          begin 
                Label100.Caption   :=   'Have   Error!'; 
          end 
          else 
          begin 
                if(filelength(handle)=0) then 
                begin                   if(RadioButton_EditWhite.Checked)then 
                      begin 
                            ch   :=   $aa; 
                            write(handle,   @ch,   1); 
                      end 
                      if(RadioButton_EditBlack.Checked) then
                      begin 
                            ch   :=   $bb; 
                            write(handle,   @ch,   1); 
                      end; 
                end;           Label100.Caption   :=   'Is   Writing...'; 
              if(write(handle,   ByteTmp,   5)=-1) then
              begin 
                    if(errno=EACCES   ) then
                        Label100.Caption   :=   'Permission   denied';
                    else   if(errno=EBADF) then
                          Label100.Caption:='   Bad   file   number'; 
                    else 
                          Label100.Caption:='   Write   file   error!'   ; 
                end 
                else 
                Label100.Caption   :=   'Have   Writed!'; 
                close(handle); 
          end; 
    end;        
      

  3.   

    把BCB中的文件操作改为了Delphi的流操作,没法调试,错误难免,大致流程应该是正确的,请调试改错。还有那个GetMyByte函数,应该是个自定义函数,没看见,所以没法给你翻译
    var
      stra: string;
      x, y, ch: Byte;
      ByteTmp: array[0..9] of Byte;
      nTmp: LongWord;
      j: Integer;
      S: TStream;
    begin
          if not (RadioButton_EditWhite.Checked or RadioButton_EditBlack.Checked) then
            Exit;
          stra := MaskEdit_WriteID.Text;
          x := Ord(stra[1]);
          y := Ord(stra[2]);
          x := x * 10 + y;
          if x > 42 then
          begin
                  MaskEdit_WriteID.Text := '0000000000';
                  Exit;
          end;
          nTmp := StrToInt64(MaskEdit_WriteID.Text);
          for j := 0 to 9 do
          begin
                ByteTmp[j] := nTmp div GetMyByte(10, 9 - j);
                nTmp := nTmp mod GetMyByte(10, 9 - j);
          end;
          for j := 0 to 4 do
                ByteTmp[j] :=  (ByteTmp[j * 2] shl 4) or (ByteTmp[j * 2 + 1] and $0f);
          S := TFileStream.Create(strWriteFileDir, fmOpenReadWrite);
          try
            try
              if S.Size = 0 then
              begin
                if RadioButton_EditWhite.Checked then
                begin
                  ch := $aa;
                  S.Write(ch, 1);
                end;
                if RadioButton_EditBlack.Checked then
                begin
                  ch := $bb;
                  S.Write(ch, 1);
                end;
                Label100.Caption := 'Is   Writing...';
                S.Write(ByteTmp, 5);
              end;
            finally
              S.Free;
            end;
          except
            on EStreamError do
              Label100.Caption := EStreamError.Message;
          end;
    end;
      

  4.   

    流操作的异常处理有错误,做一些改正
    var
      stra: string;
      x, y, ch: Byte;
      ByteTmp: array[0..9] of Byte;
      nTmp: LongWord;
      j: Integer;
      S: TStream;
    begin
          if not (RadioButton_EditWhite.Checked or RadioButton_EditBlack.Checked) then
            Exit;
          stra := MaskEdit_WriteID.Text;
          x := Ord(stra[1]);
          y := Ord(stra[2]);
          x := x * 10 + y;
          if x > 42 then
          begin
                  MaskEdit_WriteID.Text := '0000000000';
                  Exit;
          end;
          nTmp := StrToInt64(MaskEdit_WriteID.Text);
          for j := 0 to 9 do
          begin
                ByteTmp[j] := nTmp div GetMyByte(10, 9 - j);
                nTmp := nTmp mod GetMyByte(10, 9 - j);
          end;
          for j := 0 to 4 do
                ByteTmp[j] := (ByteTmp[j * 2] shl 4) or (ByteTmp[j * 2 + 1] and $0f);
        try
          S := TFileStream.Create(strWriteFileDir, fmOpenReadWrite);
          try
            try
              if S.Size = 0 then
              begin
                if RadioButton_EditWhite.Checked then
                begin
                  ch := $aa;
                  S.Write(ch, 1);
                end;
                if RadioButton_EditBlack.Checked then
                begin
                  ch := $bb;
                  S.Write(ch, 1);
                end;
                Label100.Caption := 'Is   Writing...';
                S.Write(ByteTmp, 5);
              end;
            finally
              S.Free;
            end;
          except
            on e: EStreamError do
              Label100.Caption := e.Message;
          end;
        except
          Label100.Caption := 'Have Error!'; 
        end;
      

  5.   

    谢谢大家啊!GetMyByte 的函数是下面的,麻烦再转换一下,谢谢!(感动中...)
    DWORD __fastcall TForm_PRCVersion::GetMyByte(BYTE m, BYTE n)
    {
        DWORD dwTmp;
        dwTmp = 1;
        for(BYTE i=0; i<n; i++)
           dwTmp = dwTmp * m;
        return dwTmp;
    }
      

  6.   

    function     TForm_PRCVersion.GetMyByte(m,n:Byte):DWORD ;
    var
       dwTmp:DWORD;
       i:Byte;
    begin
          
            dwTmp  :=   1; 
            for i:=0 to n-1 do
                  dwTmp   :=   dwTmp   *   m; 
            Result:=   dwTmp; 
    end; 
      

  7.   

    好人的啊,我看了都感动,这么一大段的C++ Builder的代码我估计是,真有人这么翻译了。