delphi 字符串与二进制数之间的互相转换


本文整理自网络,侵删。

 

{将一个字符串转为二进制,再从二进制转为原字符串。

   把字符串(可含中文字符)转为二进制数的函数:ConvertStrToBin();把二进制数转换为字符串的函数:ConvertBinToStr()。
   以下两个函数亦可以对包含有中文字符的字符串进行处理,逆转时亦可正常转为中文。}


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;

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()如下:

 


procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ShellExecute(handle,nil,pchar('http://www.3464.com/'),nil,nil,sw_shownormal);
end;

procedure TForm1.Button3Click(Sender: TObject);
var myint : integer;
begin
  myint := StrToInt('$' + '3A'); // myint = 58
  showmessage(ConvertStrToBin('0'));
end;

相关阅读 >>

汇编数据宽度和字节序

Delphi simple resource api replacement

Delphi nethttpclient1 数据库查询

Delphi cross socket的库

Delphi7 写过卡巴主动防御服务端

Delphi makeuniquefilename 创建文件名如果已经存在自动更改文件名

Delphi 判断tcp端口是否被占用的方法

Delphi中如何给一个字符串从左边进行补0

Delphi xe7 新的并行库 system.threading

Delphi 将被其他窗体遮住的窗体弹到最前面

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...