將一個(gè)字符串轉(zhuǎn)為二進(jìn)制,再從二進(jìn)制轉(zhuǎn)為原字符串。
把字符串(可含中文字符)轉(zhuǎn)為二進(jìn)制數(shù)的函數(shù):ConvertStrToBin();把二進(jìn)制數(shù)轉(zhuǎn)換為字符串的函數(shù):ConvertBinToStr()。
以下兩個(gè)函數(shù)亦可以對(duì)包含有中文字符的字符串進(jìn)行處理,逆轉(zhuǎn)時(shí)亦可正常轉(zhuǎn)為中文。
Function ConvertStrToBin(Value : string):string;//把字符串轉(zhuǎn)化為二進(jìn)制數(shù)
var tempHex : string[2];
i : integer;
begin
Result := '';
if trim(Value) = '' then Exit;
tempHex := '';
for i := 1 to Length(Value) do
begin
tempHex := IntToHex(Ord(Value[i]),2);//每個(gè)字符轉(zhuǎn)成兩位十六進(jìn)制數(shù)
Result := Result + BinToHexEachOther(tempHex,False);//十六進(jìn)制轉(zhuǎn)成二進(jìn)制
end;
end;
Function ConvertBinToStr(Value : string):string; //把二進(jìn)制數(shù)據(jù)轉(zhuǎn)化為字符串
Var tempHex : string;
i, tempInt : integer;
begin
Result := '';
if trim(Value) = '' then Exit;
tempHex := BinToHexEachOther(Value,true);//二進(jìn)制轉(zhuǎn)成十六進(jìn)制
i := 0;
Repeat
begin
i := i + 1;
tempInt := HexCharToInt(tempHex[i]);
i := i + 1;
tempInt := tempInt * 16 + HexCharToInt(tempHex[i]);
//以上將兩位十六進(jìn)制數(shù)轉(zhuǎn)變?yōu)橐粋€(gè)十進(jìn)制數(shù)
Result := Result + chr(TempInt); //轉(zhuǎn)成ASCII碼
end;
Until i >= length(tempHex)
end;
上兩個(gè)互逆的函數(shù)中要調(diào)用到的函數(shù)HexCharToInt()和BinToHexEachOther()如下:
function BinToHexEachOther(ValueA : string; Action : Boolean) : string;
//把二進(jìn)制串轉(zhuǎn)換成十六進(jìn)制串或相反
var
ValueArray1 : Array [0..15] of string[4];
ValueArray2 : Array [0..15] of char;
i : shortint;
begin
//數(shù)組初始化
ValueArray1[0] := '0000'; ValueArray1[1] := '0001'; ValueArray1[2] := '0010';
ValueArray1[3] := '0011'; ValueArray1[4] := '0100'; ValueArray1[5] := '0101';
ValueArray1[6] := '0110'; ValueArray1[7] := '0111'; ValueArray1[8] := '1000';
ValueArray1[9] := '1001'; ValueArray1[10] := '1010'; ValueArray1[11] := '1011';
ValueArray1[12] := '1100'; ValueArray1[13] := '1101'; ValueArray1[14] := '1110';
ValueArray1[15] := '1111';
for i := 0 to 15 do
if i >= 10 then ValueArray2[i] := chr(65 + (i - 10))
else ValueArray2[i] := inttostr(i)[1];
Result := '';
if Action then
begin //二進(jìn)制串轉(zhuǎn)換成十六進(jìn)制串
if (Length(ValueA) MOD 4 <> 0) then
ValueA := stringofchar('0',Length(ValueA) MOD 4) + ValueA;
while (Length(ValueA) >= 4) do
begin
for i := 0 to 15 do
if Copy(ValueA,1,4) = ValueArray1[i] then
Result := Result + ValueArray2[i];
ValueA := Copy(ValueA,5,Length(ValueA) - 4);
end;
end
else begin //十六進(jìn)制串轉(zhuǎn)換成二進(jìn)制串
while (Length(ValueA) >= 1) do
begin
for i := 0 to 15 do
if Copy(ValueA,1,1) = ValueArray2[i] then
Result := Result + ValueArray1[i];
ValueA := Copy(ValueA,2,Length(ValueA) - 1);
end;
end;
end;
function HexCharToInt(HexToken : char):Integer;
begin
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;
十六進(jìn)制字串轉(zhuǎn)十進(jìn)制又一法:
procedure TForm1.BitBtn1Click(Sender: TObject);
var myint : integer;
begin
myint := StrToInt('$' + '3A'); // myint = 58
showmessage(inttostr(myint));
end;