如何将一个16进制数转化为2进制。(10分)

  • 主题发起人 主题发起人 青云
  • 开始时间 开始时间

青云

Unregistered / Unconfirmed
GUEST, unregistred user!
我在编写通讯软件的时候,遇到一个小小的技术问题。比如:
var hex:string;
binary:array[0..7] of char;
------------------------------------------
hex:='7F';
现在如何实现binary='01111111'.
不知道delphi有没有现成的函数,要是没有,该如何编写。
 

function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer

Description

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.

BufSize is the size of Buffer. Text needs to point to at least 2*BufSize hexadecimal characters, because each two hexadecimal characters represent one byte.

HexToBin returns the number of characters in Buffer that have not been used because Text did not contain valid hexadecimal characters ('0'..'f').

Note: The hexadecimal number must use lower-case characters; HexToBind does not recognize upper-case characters.
 
function HexToBin(Text, Buffer: PChar; BufSize: Integer): Integer
例如:
HexToBin(Pchar(十六进制字符),PCHAR(二进制字符缓存),二进制缓存大小);
 
谢谢上面两位朋友的的讲解,不过,我还是有点不清楚,我想,你们能不能举一个具体的事例。
比如:

var hex:string;
binary:array[0..7] of char;
------------------------------------------
hex:='7F';
现在如何实现binary='01111111'.
现在如何通过hextobin这个函数实现这个功能。
 
HexToBin(Pchar(hex),binary,7);
 
kenny.hu 朋友,您的方法我调试过了,binary好象是乱码啊。
哪位朋友再介绍的具体一点好吗?
 
sorry,
干脆抄段给你
function HexToBin(HexStr: String): String;
const
BinLst1: array ['0'..'9'] of String[4]
= ('0000','0001','0010','0011','0100','0101','0110','0111','1000','1001');
BinLst2: array ['A'..'F'] of String[4]
= ('1010','1011','1100','1101','1110','1111');
HexChars: set of char = ['0'..'9', 'A'..'F', 'a'..'f'];
var
I, X: Integer;
ch: Char;
begin
X:= 0; Result:= '';
for I:= 1 to Length(HexStr) do
if HexStr in HexChars then begin
X:= I; Break;
end;
if X = 0 then Exit;
for I:= X to length(HexStr) do begin
ch:= upcase(HexStr);
case ch of
'0'..'9': Result:= Result + BinLst1[ch];
'A'..'F': Result:= Result + BinLst2[ch];
else Break;
end;{case}
end;
end;
 
误区:

Delphi 自带的 HexToBin 是将一个数值的十六进制表达的字符串转换成 ASCII 码,而非二进制表达式。

例如:

procedure TForm1.Button1Click(Sender: TObject);
var
hex:string;
binary:array[0..7] of char;
begin
hex:='414243444546';
HexToBin(PChar(hex),binary,8);
caption:='';
Label1.caption:=binary; //显示:'ABCDEF','A'的ASCII值用16进制表达是$41,'B'是$42...
end;

要将一个整数用二进制字符串表达,可以:

function IntToBin(Value: LongInt;Size: Integer): String;
var
i: Integer;
begin
Result:='';
for i:=Size-1 downto 0 do
begin
if Value and (1 shl i)<>0 then
Result:=Result+'1'
else
Result:=Result+'0';
end;
end;

例:

procedure TForm1.Button1Click(Sender: TObject);
var
hex:string;
begin
hex:='7f';
caption:=IntToBin(strtoint('$'+hex),8); //显示:01111111
end;
 
笔误, 楼上 caption:=''; 一句应取消。
 
大家提供的帮助让我受益非浅,现在我想在问一下,如何将二进制转换为16进制呢?
 
function BinToInt(Value: String): LongInt;
var
i,Size: Integer;
begin
Result:=0;
Form1.Caption:='';
Size:=Length(Value);
for i:=1 to size do begin
if Copy(Value,i,1)='1' then begin
Result:=Result+(1 shl (size-i));
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
caption:='$'+inttohex(BinToInt('01111111'),2);
end;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部