请各位帮我编一个能够把一个字符串变成二进制的函数,字符串都是由1和o组成的一共16位,例如1100001001011010,这个函数能够把形如这样的字符串变成和他表示的二进制数数值一样的二进制数。

解决方案 »

  1.   

    你想做什么啊,delphi里的2进制本来就有点象字符串啊
      

  2.   

    先得到ASCII码,再转换成二进制
      

  3.   

    s:='1100001001011010';
    c:=0;
    for i:=1 to length(s) do
    begin
      c:=c*2+inttostr(s[i]);
    end;
      

  4.   

    阿琪的实现得到int
    再inttobin函数
      

  5.   

    c:=c*2+inttostr(s[i]);--->c:= c*2 + ord(s[i]) -$30
      

  6.   

    uses IdGlobal;var
          vStr : String;
          iSum, I, J, temp : Integer;
    begin
          vStr := '1100001001011010';
          iSum := 0;
          for I := 1 to Length(vStr) do
          begin
                if vStr[I] = '1' then
                begin
                      temp := 1;
                      for J := Length(vStr) - 1 downto I do
                      temp := temp * 2;
                      iSum := iSum + temp;
                end else Continue;
          end;
          Result := Copy(IntToBin(iSum), 16 + 1, 16);
    end;
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
     i,sum:integer;
     st:string;
    begin
     st:='1100001001011010';
     sum:=0;
     for i:=1 to length(st)-1 do
      begin
       inc(sum,strtoint(st[i]));
       sum:=sum*2;
      end;
      inc(sum,strtoint(st[length(st)]));
     button1.Caption :=inttostr(sum)+'  '+inttobin(sum);
    end;