急急急(100分)

  • 主题发起人 主题发起人 swj
  • 开始时间 开始时间
S

swj

Unregistered / Unconfirmed
GUEST, unregistred user!
我要写的程序要对二进制的数字进行操作
但delphi中有inttohex 有没有直接到二进制的函数
而且存放的二进制有bcd码和8421两种,转化时怎么办?
 
好象没有,只能自己写
 
难道对二进制进行操作就非要把他转成二进制么?在16进制时进行与或者
或操作就是逐位进行的呀,我觉得应该够了,
 
怎么对16进制操作啊
我知道有inttohex是把一个数转变成16进制的字符串
但好像没有倒过来的函数
怎么将这个过程倒过来呢?
 
//inttohex是把一个数转变成16进制的字符串 怎么将这个过程倒过来呢?

function HexToInt(hex: string): integer;
var
i, value: integer;
begin
Result := 0;
hex := upercase(hex);
for i := Length(hex) downto 1 do
begin
if (hex >= 'A') and (hex <= 'F') then
value := ord(hex) - ord('A') + 10
else
value := ord(hex) - ord('0');
Result := Result * 16 + value;
end;
end;
//现写的,没有验证过,你可以试一试
 
抄一段给你:
unit BinHexTools;

interface
function HexToBin(HexNr : string): string; //把十六进制字符串转换为二进制字符串
function HexCharToInt(HexToken : char):Integer; //转换一个十六进制字符为整数
function HexCharToBin(HexToken : char): string;//转换一个十六进制字符为二进制字符串
function pow(base, power: integer): integer;//指数函数
function BinStrToInt(BinStr : string) : integer;//把二进制字符串转换为整数
function DecodeSMS7Bit(PDU : string):string;// 解码一个7-bit SMS (GSM 03.38) 为ASCII码
function ReverseStr(SourceStr : string) : string; //反转一个字符串

implementation
uses sysutils, dialogs;

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;

function HexToBin(HexNr : string): string;
{ only stringsize is limit of binnr }
var Counter : integer;
begin
Result:='';

for Counter:=1 to length(HexNr) do
Result:=Result+HexCharToBin(HexNr[Counter]);
end;

function pow(base, power: integer): integer;
var counter : integer;
begin
Result:=1;

for counter:=1 to power do
Result:=Result*base;
end;

function BinStrToInt(BinStr : string) : integer;
var counter : integer;
begin
if length(BinStr)>16 then
raise ERangeError.Create(#13+BinStr+#13+
'is not within the valid range of a 16 bit binary.'+#13);

Result:=0;

for counter:=1 to length(BinStr) do
if BinStr[Counter]='1' then
Result:=Result+pow(2,length(BinStr)-counter);
end;

function DecodeSMS7Bit(PDU : string):string;
var OctetStr : string;
OctetBin : string;
Charbin : string;
PrevOctet: string;
Counter : integer;
Counter2 : integer;
begin
PrevOctet:='';
Result:='';

for Counter:=1 to length(PDU) do
begin
if length(PrevOctet)>=7 then { if 7 Bit overflow on previous }
begin
if BinStrToInt(PrevOctet)<>0 then
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+' ';

PrevOctet:='';
end;

if Odd(Counter) then { only take two nibbles at a time }
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);

Charbin:='';
for Counter2:=1 to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];

for Counter2:=1 to 7-length(PrevOctet) do
Charbin:=OctetBin[8-Counter2+1]+Charbin;

if BinStrToInt(Charbin)<>0 then Result:=Result+Chr(BinStrToInt(CharBin))
else Result:=Result+' ';

PrevOctet:=Copy(OctetBin,1,length(PrevOctet)+1);
end;
end;
end;

function ReverseStr(SourceStr : string) : string;
var Counter : integer;
begin
Result:='';

for Counter:=1 to length(SourceStr) do
Result:=SourceStr[Counter]+Result;
end;

end.



 
多人接受答案了。
 
后退
顶部