konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks
          將一個字符串轉為二進制,再從二進制轉為原字符串。

             把字符串(可含中文字符)轉為二進制數的函數: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;
          posted on 2005-11-02 05:37 konhon 優華 閱讀(22517) 評論(3)  編輯  收藏 所屬分類: Delphi

          Feedback

          # re: 字符串與二進制數之間的互相轉換 2007-09-20 22:03 burgess
          謝謝樓主  回復  更多評論
            

          # re: 字符串與二進制數之間的互相轉換 2009-03-05 08:38 啊啊啊
          非常不錯!寫的好,受益良多  回復  更多評論
            

          # re: 字符串與二進制數之間的互相轉換 [未登錄] 2013-05-31 23:28 aaa
          CF79AE6ADDBA60AD018347359BD144D2  回復  更多評論
            

          主站蜘蛛池模板: 屯昌县| 岳普湖县| 上林县| 那坡县| 承德县| 长武县| 阜阳市| 女性| 全椒县| 霍林郭勒市| 平山县| 察雅县| 孝昌县| 桃园县| 银川市| 宿松县| 安多县| 乌海市| 高青县| 涿州市| 博兴县| 神木县| 金坛市| 北川| 五常市| 灵山县| 南投市| 苏州市| 平阳县| 泰安市| 保康县| 太康县| 百色市| 孝感市| 滨州市| 汾西县| 尼玛县| 连城县| 出国| 区。| 潍坊市|