Call HexToBin to convert the hexadecimal string Text to the binary value it represents.Text is a string representation of a hexadecimal value.Buffer returns the resulting value in binary
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer
var E: Extended; //Make sure there is room for null terminator Buf: array[0..SizeOf(Extended) * 2] of Char; begin E := Pi; Label1.Caption := Format('Pi starts off as %.15f', [E]); BinToHex(@E, Buf, SizeOf(E)); //Slot in the null terminator for the PChar, so we can display it easily Buf[SizeOf(Buf) - 1] := #0; Label2.Caption := Format('As text, the binary contents of Pi look like %s', [Buf]); //Translate just the characters, not the null terminator HexToBin(Buf, @E, SizeOf(Buf) - 1); Label3.Caption := Format('Back from text to binary, Pi is now %.15f', [E]); end;
直覺以為 HexToBin 是將一個十六進制字符串的字符轉為一個字符串的, 可能是英語太差了!! 測試一下卻不是!! 是直接轉為一個數值型的! 我不知道我有沒有理解錯!! 請大家討論; 然後, 如果要用到如我一開始提到的功能, 可用如下: procedure TForm1.Button3Click(Sender: TObject); function HextoBin1(Hex:string):string; const BOX: array [0..15] of string = ('0000','0001','0010','0011', '0100','0101','0110','0111', '1000','1001','1010','1011', '1100','1101','1110','1111'); var i:integer; begin for i:=Length(Hex) downto 1 do Result:=BOX[StrToInt('$'+Hex[i])]+Result; end; begin Edit1.Text := HextoBin1('FD'); end;
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer例子procedure TForm1.Button1Click(Sender: TObject); var Text, Buffer: PChar; Len:Integer; Bin : array[0..111] of char ; begin Text:='356555'; Len:=lENGTH(Text) DIV 2; HexToBin(Text, Bin, LEN); edit1.Text:=Bin; end;end.
E: Extended;
//Make sure there is room for null terminator
Buf: array[0..SizeOf(Extended) * 2] of Char;
begin
E := Pi;
Label1.Caption := Format('Pi starts off as %.15f', [E]);
BinToHex(@E, Buf, SizeOf(E));
//Slot in the null terminator for the PChar, so we can display it easily
Buf[SizeOf(Buf) - 1] := #0;
Label2.Caption := Format('As text, the binary contents of Pi look like %s', [Buf]);
//Translate just the characters, not the null terminator
HexToBin(Buf, @E, SizeOf(Buf) - 1);
Label3.Caption := Format('Back from text to binary, Pi is now %.15f', [E]);
end;
測試一下卻不是!! 是直接轉為一個數值型的!
我不知道我有沒有理解錯!! 請大家討論;
然後, 如果要用到如我一開始提到的功能, 可用如下:
procedure TForm1.Button3Click(Sender: TObject);
function HextoBin1(Hex:string):string;
const
BOX: array [0..15] of string =
('0000','0001','0010','0011',
'0100','0101','0110','0111',
'1000','1001','1010','1011',
'1100','1101','1110','1111');
var
i:integer;
begin
for i:=Length(Hex) downto 1 do
Result:=BOX[StrToInt('$'+Hex[i])]+Result;
end;
begin
Edit1.Text := HextoBin1('FD');
end;
var
Text, Buffer: PChar;
Len:Integer;
Bin : array[0..111] of char ;
begin
Text:='356555';
Len:=lENGTH(Text) DIV 2;
HexToBin(Text, Bin, LEN);
edit1.Text:=Bin;
end;end.