如何将string转成它的二进制码显示 ( 积分: 100 )

  • 主题发起人 主题发起人 birdhzx
  • 开始时间 开始时间
B

birdhzx

Unregistered / Unconfirmed
GUEST, unregistred user!
要不显示的都是乱码,感谢
 
要不显示的都是乱码,感谢
 
用BinToHex函数就可以了,示例如下:
procedure TForm1.Button1Click(Sender: TObject);
var
Str:string;
Bin:string;
begin
Str:='ABCabc';
SetLength(Bin,Length(Str)*2);
BinToHex(PChar(Str),PChar(Bin),Length(Str));
ShowMessage(Bin);
end;
 
function HexToBin(HexNr: String): String;
Var
Counter: integer;
Function HexCharToInt(HexToken: char): Integer;
Begin
If HexToken > #97 Then
HexToken := Chr(Ord(HexToken) - 32);
{ use lowercase aswell }

Result := 0;

If (HexToken > #47) And (HexToken < #58) Then { chars 0....9 }
Result := Ord(HexToken) - 48
Else If (HexToken > #64) And (HexToken < #71) Then { chars A....F }
Result := Ord(HexToken) - 65 + 10;
End;
Function HexCharToBin(HexToken: char): String;
Var
DivLeft: integer;
Begin
DivLeft := HexCharToInt(HexToken); { first HEX->BIN }
Result := '';
{ Use reverse dividing }
Repeat { Trick; divide by 2 }
If odd(DivLeft) Then { result = odd ? then bit = 1 }
Result := '1' + Result { result = even ? then bit = 0 }
Else
Result := '0' + Result;

DivLeft := DivLeft Div 2; { keep dividing till 0 left and length = 4 }
Until (DivLeft = 0) And (length(Result) = 4); { 1 token = nibble = 4 bits }
End;
Begin
Result := '';

For Counter := 1 To length(HexNr) Do
Result := Result + HexCharToBin(HexNr[Counter]);
End;
 
啊哦
var
p: pbyte;
p:=pchar(s);
//do loop
intobin(p^);
 
后退
顶部