konhon

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

          Google

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

             把字符串(可含中文字符)轉(zhuǎn)為二進(jìn)制數(shù)的函數(shù):ConvertStrToBin();把二進(jìn)制數(shù)轉(zhuǎn)換為字符串的函數(shù):ConvertBinToStr()。
             以下兩個函數(shù)亦可以對包含有中文字符的字符串進(jìn)行處理,逆轉(zhuǎn)時亦可正常轉(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);//每個字符轉(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)橐粋€十進(jìn)制數(shù)
                Result := Result + chr(TempInt); //轉(zhuǎn)成ASCII碼
              end;
            Until i >= length(tempHex)
          end;

          上兩個互逆的函數(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;
          posted on 2005-11-02 05:37 konhon 優(yōu)華 閱讀(22526) 評論(3)  編輯  收藏 所屬分類: Delphi

          Feedback

          # re: 字符串與二進(jìn)制數(shù)之間的互相轉(zhuǎn)換 2007-09-20 22:03 burgess
          謝謝樓主  回復(fù)  更多評論
            

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

          # re: 字符串與二進(jìn)制數(shù)之間的互相轉(zhuǎn)換 [未登錄] 2013-05-31 23:28 aaa
          CF79AE6ADDBA60AD018347359BD144D2  回復(fù)  更多評論
            

          主站蜘蛛池模板: 定兴县| 新干县| 涪陵区| 娱乐| 迁西县| 东明县| 叶城县| 包头市| 岳西县| 夏邑县| 三台县| 甘孜| 新绛县| 深泽县| 舟曲县| 西乌珠穆沁旗| 新宁县| 灵川县| 青阳县| 杭锦旗| 开原市| 乌什县| 平利县| 广饶县| 商河县| 临夏市| 绍兴县| 彝良县| 武平县| 广德县| 洛南县| 江都市| 德格县| 微山县| 松桃| 南平市| 独山县| 施秉县| 布拖县| 廉江市| 毕节市|