admin 管理员组

文章数量: 887021


2024年1月26日发(作者:前端程序员工资)

怎么把一个字符串转换成16进制数据,然后再把16进制数转成字符串?

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 { 9 }

Result:=Ord(HexToken)-48

else if (HexToken>#64) and (HexToken<#71) then { 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 (#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;

16进制的就是STRING类型的!

要完成此效果需要两个步骤

第一步:把如下代码加入到 区域中

第二步:把如下代码加入到 区域中

输入一个字符:

ASCII码十进制输出

ASCII码十六进制输出

ASCII码二进制输出


本文标签: 字符串 二进制 转换 字符 进制