二进制与十六进制之间的转换? (20分)

  • 主题发起人 主题发起人 jmlwz
  • 开始时间 开始时间
看看别人是怎样做的吧
http://www.playicq.com/dispdocnew.php?id=8250
 
把十六进制字符串转换为二进制字符串
Function HexToBin(HexNr: String): String;
{ only stringsize is limit of binnr }
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;
 
http://delphibbs.com/delphibbs/dispq.asp?lid=2182012
 
function OcttoBin(i: integer): string;
//十进制转换为二进制字符串
var
j: integer;
s: string;
begin
j := i;
s := ' ';
while j &amp;gt;= 2do
begin
if (j mod 2) = 1 then
begin
s := '1' + s;
j := j div 2;
end
else
begin
s := '0' + s;
j := j div 2;
end;
end;
s := chr(ord('0') + j) + s;
result := s;
end;

function BintoOct(k: string): integer;
//二进制字符串转换为十进制
var
i, j, t: integer;
s: char;
begin
t := 1;
j := length(k);
j := 0 + (ord(k[j]) - ord('0')) * t;
;
for i := length(k) - 1do
wnto 1do
begin
s := k;
t := t * 2;
j := j + ((ord(s) - ord('0')) * t);
end;
result := j;
end;

function OcttoHex(i: integer): string;
//十进制转换为四位的十六进制字符串
begin
result := InttoHex(i, 4);
end;

function HextoOct(k: string): integer;
//十六进制字符串转换为十进制数
begin
result := StrtoInt('$' + k);
end;

function HextoBin(k: string): string;
//十六进制字符串转换为二进制字符串
begin
result := OcttoBin(HextoOct(k));
end;

function BintoHex(k: string): string;
//二进制字符串转换为四位的十六进制字符串
begin
result := Octtohex(BintoOct(k));
end;
 
在DELPHI里是不是有这个函数?
procedure BinToHex(Buffer, Text: PChar;
BufSize: Integer);
assembler;
参数应该怎么写?
 
第一个参数是二进制字符串
第二个参数是得到的十六进制字符串
第三个参数是Length(buffer),二进制字符串的长度.
十六进制字符串的长度是二进制字符串长度的两倍(看看它的源代码就清楚了).
 
接受答案,并分配积分
 
后退
顶部