把字串(可含中文字元)轉為位元的函數:ConvertStrToBin();把位元轉換為字串的函數:ConvertBinToStr()。
以下兩個函數亦可以對包含有中文字元的字串進行處理,逆轉時亦可正常轉為中文。
Function ConvertStrToBin(Value : string):string;//把字串轉化為位元
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);//每個字元轉成兩位十六進位數
Result := Result + BinToHexEachOther(tempHex,False);//十六進位轉成二進位
end;
end;
Function ConvertBinToStr(Value : string):string; //把位元據轉化為字串
Var tempHex : string;
i, tempInt : integer;
begin
Result := '';
if trim(Value) = '' then Exit;
tempHex := BinToHexEachOther(Value,true);//二進位轉成十六進位
i := 0;
Repeat
begin
i := i + 1;
tempInt := HexCharToInt(tempHex[i]);
i := i + 1;
tempInt := tempInt * 16 + HexCharToInt(tempHex[i]);
//以上將兩位十六進位數轉變為一個十進位數
Result := Result + chr(TempInt); //轉成ASCII碼
end;
Until i >= length(tempHex)
end;
上兩個互逆的函數中要調用到的函數HexCharToInt()和BinToHexEachOther()如下:
function BinToHexEachOther(ValueA : string; Action : Boolean) : string;
//把二進位串轉換成十六進位串或相反
var
ValueArray1 : Array [0..15] of string[4];
ValueArray2 : Array [0..15] of char;
i : shortint;
begin
//數組初始化
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 //二進位串轉換成十六進位串
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 //十六進位串轉換成二進位串
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;
處理效果如圖所示
///////////////////////////////////////////////////////////////////////////////////
學了一招,這個的確省事多了。。。。
procedure TForm1.BitBtn1Click(Sender: TObject);
var myint : integer;
begin
myint := StrToInt('$' + '3A'); // myint = 58
showmessage(inttostr(myint));
end;
/////////////////////////////////////////////////////////////////////////////////////////////////////////
需要把尾碼名為*.bin 的檔案以二進位形式讀入(需要顯示在Memo 框中)
然後可以修改並能儲存成 bin 檔案。
==================================================================
這是讀取.bin並顯示在Tmemo框的代碼:
其中,OpenDialog1.Options := [ofHideReadOnly,ofPathMustExist,ofFileMustExist,ofEnableSizing];
OpenDialog1.Filter := '*.bin|*.bin';
OpenDialog1.DefaultExt := '*.bin';
procedure TForm1.Button1Click(Sender: TObject);
var
f : file of byte;
fs,i : Integer;
bytes : array of byte;
tempstr : string;
begin
if OpenDialog1.Execute then
begin
tempstr := '';
assignfile(f,OpenDialog1.FileName);
reset(f);
fs:=filesize(f);//返回的位元組數正確
SetLength(bytes,fs);
i := 0;
repeat
seek(f,i);
read(f,bytes[i]);
tempstr := tempstr + ' ' + IntToHex(Bytes[i],2);
Inc(i);
until i >= fs;
tempstr := Copy(tempstr,2,Length(tempstr));
Memo1.Text := tempstr;
CloseFile(f);
end;
end;
==================================================================
這是把Tmemo框內顯示的二進位檔案寫入指定檔案名稱的.bin檔案裡的代碼:
其中SaveDialog1.Options := [ofOverwritePrompt,ofHideReadOnly,ofPathMustExist,ofEnableSizing];
SaveDialog1.Filter := '*.bin|*.bin';
SaveDialog1.DefaultExt := '*.bin';
procedure TForm1.Button2Click(Sender: TObject);
var
f : TFileStream;
data : string;
Buf: PChar;
begin
data := Memo1.Text;
data := StringReplace(data,' ','',[rfReplaceAll]);//去除空格
if SaveDialog1.Execute then
begin
GetMem(Buf,Length(data) div 2);
HexToBin(@data[1],Buf,Length(data) div 2); //十六進位字串轉換成二進位
f := TFileStream.Create(SaveDialog1.FileName, fmCreate);
try
f.Seek(0, soFromBeginning);
f.Write(Buf^, Length(data) div 2);
finally
f.Free;
end;
FreeMem(Buf);
end;
end;
再通過讀寫.bin檔案,把儲存起來的.bin重新顯示到TMemo框中去,發現顯示結果與原檔案一模一樣;如果需要修改待儲存檔案,則可以在TMemo框中修改再行儲存,重新顯示的結果也是正確的。