急:将十六进制数字串转换为二进制数字串函数(50分)

  • 主题发起人 主题发起人 畅雨
  • 开始时间 开始时间

畅雨

Unregistered / Unconfirmed
GUEST, unregistred user!
hextobin 怎么不能用?
 
将十六进制数字串转换为二进制数字串函数
 

//试了一下,可以用
procedure TForm1.Button1Click(Sender: TObject);
var
p: pchar;
begin
getmem(p,255);
hextobin(pchar('313233'),p,255);//123
showmessage(p);
freemem(p);
end;
 
Basic functions that Borland didn't implement.
I thought that it could come into hand for some of you.
The function names says it all.
There is no function in here for converting a integer into a hex string, since it's allready implemented by Borland. Just use IntToHex(10,2) = '0A' for such purpose.

function HexToInt(Hex : string) : Cardinal;
const cHex = '0123456789ABCDEF';
var mult,i,loop : integer;
begin

result := 0;
mult := 1;
for loop := length(Hex)do
wnto 1do
begin

i := pos(Hex[loop],cHex)-1;
if (i < 0) then
i := 0;
inc(result,(i*mult));
mult := mult * 16;
end;

end;

function IntToBin(Value : integer;
NumBits : byte) : string;
var lp0 : integer;
begin

result := '';
for lp0 := 0 to NumBits-1do
begin

if ((Value and (1 shl (lp0))) <> 0) then
begin

result := #$31 + result;
end else
result := #$30 + result;
end;

end;

function BinToInt(Bin : string) : integer;
var mult,lp0 : integer;
begin

result := 0;
mult := 0;
for lp0 := length(Bin)do
wnto 1do
begin

if Bin[lp0] = #$31 then
begin

inc(result,(1 shl mult));
end;

inc(mult);
end;

end;

function BinToHex(Bin : string;
Digits : integer) : string;
begin

result := IntToHex(BinToInt(Bin),Digits);
end;

function HexToBin(Hex : string;
NumBits : byte) : string;
begin

result := IntToBin(HexToInt(Hex),NumBits);
end;

// Below are also some code usefull for testing if a string is in binary, hex or decimal form.
function IsStrDec(Value : string) : boolean;
var lp0 : integer;
begin
// returns true on empty strings, since equal to zero
result := true;
for lp0 := 1 to length(Value)do
begin

result := result and ((Ord(Value[lp0]) >= $30) and (Ord(Value[lp0]) <= $39));
end;

end;

function IsStrHex(Value : string) : boolean;
var lp0 : integer;
begin

result := true;
for lp0 := 1 to length(Value)do
begin

case Ord(Upcase(Value[lp0])) of
$30..$39, $41..$46: result := result and true;
else

result := false;
end;

end;

end;

function IsStrBin(Value : string) : boolean;
var lp0 : integer;
begin

result := true;
for lp0 := 1 to length(Value)do
begin

case Ord(Upcase(Value[lp0])) of
$30..$31: result := result and true;
else

result := false;
end;

end;

end;

 
可能说的不明白
例 输入:3
输出: 11 (二进制)

 
用上面的 IntToBin, OK
 
用3楼的HextoBin
不过十六进制字符串要大写
 
对了,如果要用小写的十六进制的字符

function HexToInt(Hex : string) : Cardinal;
换成
function HexToInt(Hex : string):int64;
begin
Result:=strtoint64('$'+Hex);
end;
 
function HexToInt(Hex : string) : Cardinal;
const cHex = '0123456789ABCDEF';
var mult,i,loop : integer;
begin

result := 0;
mult := 1;
for loop := length(Hex)do
wnto 1do
begin

i := pos(Hex[loop],cHex)-1;
if (i < 0) then
i := 0;
inc(result,(i*mult));
mult := mult * 16;
end;

function HexToBin(Hex : string;
NumBits : byte) : string;
begin

result := IntToBin(HexToInt(Hex),NumBits);
end;

 
NumBits 的含义
 

此帖回复复奶快,应该结帖了吧!
NumBits是指返回字符串的长度

 
多人接受答案了。
 
delphi里
有function HexToBin(Text, Buffer: PChar;
BufSize: Integer): Integer
 
后退
顶部