<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    konhon

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

    Google

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

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

    上兩個(gè)互逆的函數(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)華 閱讀(22515) 評(píng)論(3)  編輯  收藏 所屬分類: Delphi

    Feedback

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

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

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

    主站蜘蛛池模板: 国产成人综合久久精品免费| 免费A级毛片无码A∨中文字幕下载| 亚洲卡一卡2卡三卡4麻豆| 亚洲美免无码中文字幕在线| 亚洲精品中文字幕无乱码| 青青免费在线视频| 无码国产精品一区二区免费vr| 无码国产精品一区二区免费I6| 免费一级黄色毛片| 91亚洲精品自在在线观看| 亚洲免费中文字幕| 国产精品亚洲mnbav网站 | 免费观看成人毛片a片2008| 日韩亚洲精品福利| 亚洲伊人久久大香线蕉苏妲己| 亚洲av午夜电影在线观看| 国精产品一区一区三区免费视频| 91在线视频免费91| 亚洲色欲啪啪久久WWW综合网| 91在线视频免费观看| 日日操夜夜操免费视频| 亚洲午夜精品一区二区公牛电影院 | 日韩欧毛片免费视频| 亚洲精品乱码久久久久66| 亚洲性色精品一区二区在线| 国产精品四虎在线观看免费| 日本中文字幕免费看| 成年男女免费视频网站| 亚洲人成在线电影| 国产啪精品视频网站免费尤物| 久久精品亚洲精品国产色婷| 成年女人喷潮毛片免费播放| 日日躁狠狠躁狠狠爱免费视频| 好男人视频社区精品免费| 亚洲国产av美女网站| 国产无遮挡吃胸膜奶免费看| 91精品成人免费国产| 亚洲一区二区三区91| 最近免费中文字幕高清大全| 久久夜色精品国产亚洲AV动态图 | 亚洲人成无码网站在线观看|