this slowpoke moves

Binary < > String

function DualToStr(dual: String): String;
var
  i: integer;
  b: Byte;
  pcIn, pcOut: PChar;
begin
  SetLength(Result, Length(dual) div 8);
  pcIn := @dual[1];
  pcOut := @Result[1];
  b := 0;
  for i := 1 to Length(dual) do
  begin
    b := b shl 1 + Ord(pcIn^ <> '0');
    if i mod 8 = 0 then
    begin
      pcOut^ := Chr(b);
      b := 0;
      Inc(pcOut);
    end;
    Inc(pcIn);
  end;
end;

function AnsiStringToBinaryString(const s: AnsiString): String;
const
  SBits: array[0..1] of string = ('0', '1');
var
  i, k, t: Integer;
  schar: string;
begin
  Result := '';
  for i := 1 to Length(s) do begin
    t := Ord(s[i]);
    schar := '';
    for k := 1 to 8 * SizeOf(AnsiChar) do begin
      schar := SBits[t mod 2] + schar;
      t := t div 2
    end;
    Result := Result + schar;
  end;
end;
String to Binary :
procedure TForm1.Button1Click(Sender: TObject);
begin
  //s := Memo1.Text;
  Memo1.Text :=  AnsiStringToBinaryString(Memo1.Text);
  //Memo1.Text := BitStrToCharStr(Memo1.Text);
end;
Binary to String :
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo2.Text := DualToStr(Memo1.Text);
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate