如何让字符串与二进制互相转换?
把字符串(可含中文字符)转为二进制数的函数: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框中修改再行保存,重新显示的结果也是正确的。