我想把指定的2进制值(字符串,如010011)写入2进制文件,并能够再从文件中读出并显示出来怎么做哪?劳驾请写清楚一点,新手,谢谢.

解决方案 »

  1.   

    function BinToInt(s:String):Byte;
    var
        i,n:Integer;
    begin
        if Length(s)<8 then n=Length(s) else n=8;
        result:=0;
        for i:=n downto 1 do
            result:=(result shl 1) and StrToInt(s[i]);
    end;function IntToBin(x:Byte):String;
    var
        i:Integer;
    begin
        result:='';
        for i:=1 to 8 do
        begin
            result:=result+IntToStr(x and 1);
            x:=x shr 1;
        end;
    end;var
        s:String;
        b:Byte;
        i,n:Integer;
        f:file of Byte;写文件的程序:
        //打开和关闭文件的代码就省略了!
        s='101010100101';
        i:=Length(s)+1;
        while i>1 do
        begin
            if i<8 then 
                n:=0
            else
                n:=i-8;
            Write(f,BinToInt(Copy(s,i-8,i-n));
            i:=i-8;
        end;
    读文件的程序:
        s:='';
        while not Eof(f) do
        begin
            Read(f,b);
            s:=IntToBin(b)+s;
        end;