如何将字符串反转到 bin?

发布于 2024-11-18 00:57:39 字数 497 浏览 1 评论 0原文

如何将 bin 转换为字符串? 例如:

 string:='s';----------->bin:='0011';

如何反向转换?

我的 stringtobin 代码是:

function StrToBinStr( aString: string ): string;
var
i : integer;
begin
for i := 1 to Length( aString ) do
result := IntToBin( byte(aString[i]), 4 );
end;

function IntToBin(aValue, Bits: integer): string;
var
i : integer;
begin
for i := Bits-1 downto 0 do
result := result + Copy( '10', Word(((1 shl i) and AValue) = 0)+1, 1 );
end;

How can I convert bin to string?
For example:

 string:='s';----------->bin:='0011';

How do I convert it reverse?

My stringtobin code is:

function StrToBinStr( aString: string ): string;
var
i : integer;
begin
for i := 1 to Length( aString ) do
result := IntToBin( byte(aString[i]), 4 );
end;

function IntToBin(aValue, Bits: integer): string;
var
i : integer;
begin
for i := Bits-1 downto 0 do
result := result + Copy( '10', Word(((1 shl i) and AValue) = 0)+1, 1 );
end;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2024-11-25 00:57:39

这可能会有所帮助:

function  IntToBin( const Value: LongInt; Digits: Byte;
  const Spaces: Boolean ): AnsiString;
begin
  if Digits > 32 then
    Digits := 32;
  SetLength( Result, Digits );
  Result := '';
  while Digits > 0 do
  begin
    if (Spaces) and ((Digits mod 8) = 0) then
      Result := Result + #32;
    Dec(Digits, 1);
    Result := Result + IntToStr((Value shr Digits) and 1);
  end;
end;

function BinToInt( Value: AnsiString ): LongInt;
var
  cTmp: AnsiChar;
  liCtr, liLen: LongInt;
begin
  Value := AnsiString(StringReplace(Value, #32, '', [rfReplaceAll]));
  liLen := Length(Value);
  cTmp := Value[liLen];
  Dec(liLen);
  Result := StrToInt(cTmp);
  liCtr := 1;
  while liLen > 0 do
  begin
    cTmp := Value[liLen];
    Dec( liLen );
    Result := Result + (StrToInt(cTmp) shl liCtr );
    Inc(liCtr);
  end;
end;

示例使用:

procedure TForm1.FormShow(Sender: TObject);
var
  TestStr: AnsiString;
  i: Integer;
  Temp: AnsiString;
begin
  TestStr := 'ABC';
  Temp := '';
  for i := 1 to Length(TestStr) do
    Temp := Temp + IntToBin(Ord(AnsiChar(TestStr[i])), 8, False);
  ShowMessage('Temp = ' + Temp);

  TestStr := '';
  i := 1;
  while i < Length(Temp) do
  begin
    TestStr := TestStr + AnsiChar(BinToInt(Copy(Temp, i, 8)));
    Inc(i, 8);
  end;
  ShowMessage('TestStr = ' + TestStr);
end;

正如我在对您最初问题的评论中所说,我认为这是一个糟糕的想法,但这些有效。

This may help:

function  IntToBin( const Value: LongInt; Digits: Byte;
  const Spaces: Boolean ): AnsiString;
begin
  if Digits > 32 then
    Digits := 32;
  SetLength( Result, Digits );
  Result := '';
  while Digits > 0 do
  begin
    if (Spaces) and ((Digits mod 8) = 0) then
      Result := Result + #32;
    Dec(Digits, 1);
    Result := Result + IntToStr((Value shr Digits) and 1);
  end;
end;

function BinToInt( Value: AnsiString ): LongInt;
var
  cTmp: AnsiChar;
  liCtr, liLen: LongInt;
begin
  Value := AnsiString(StringReplace(Value, #32, '', [rfReplaceAll]));
  liLen := Length(Value);
  cTmp := Value[liLen];
  Dec(liLen);
  Result := StrToInt(cTmp);
  liCtr := 1;
  while liLen > 0 do
  begin
    cTmp := Value[liLen];
    Dec( liLen );
    Result := Result + (StrToInt(cTmp) shl liCtr );
    Inc(liCtr);
  end;
end;

Sample use:

procedure TForm1.FormShow(Sender: TObject);
var
  TestStr: AnsiString;
  i: Integer;
  Temp: AnsiString;
begin
  TestStr := 'ABC';
  Temp := '';
  for i := 1 to Length(TestStr) do
    Temp := Temp + IntToBin(Ord(AnsiChar(TestStr[i])), 8, False);
  ShowMessage('Temp = ' + Temp);

  TestStr := '';
  i := 1;
  while i < Length(Temp) do
  begin
    TestStr := TestStr + AnsiChar(BinToInt(Copy(Temp, i, 8)));
    Inc(i, 8);
  end;
  ShowMessage('TestStr = ' + TestStr);
end;

As I said in my comment to your original question, I think this is a terrible idea, but these work.

请别遗忘我 2024-11-25 00:57:39
function _ConvertHexToWideString(AHex: AnsiString): WideString;
var wBinaryStream: TMemoryStream;
begin
  try
    wBinaryStream := TMemoryStream.Create;

    try
      wBinaryStream.Size := Length(AHex) div 2;

      if wBinaryStream.Size > 0 then
        HexToBin(PAnsiChar(AHex), wBinaryStream.Memory, wBinaryStream.Size);
    except
    end;

    SetString(Result, PWideChar(wBinaryStream.Memory), wBinaryStream.Size div SizeOf(WideChar));
  finally
    FreeAndNil(wBinaryStream);
  end;
end;
function _ConvertHexToWideString(AHex: AnsiString): WideString;
var wBinaryStream: TMemoryStream;
begin
  try
    wBinaryStream := TMemoryStream.Create;

    try
      wBinaryStream.Size := Length(AHex) div 2;

      if wBinaryStream.Size > 0 then
        HexToBin(PAnsiChar(AHex), wBinaryStream.Memory, wBinaryStream.Size);
    except
    end;

    SetString(Result, PWideChar(wBinaryStream.Memory), wBinaryStream.Size div SizeOf(WideChar));
  finally
    FreeAndNil(wBinaryStream);
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文