怎样把一个字符串转换成二进制,并存放在一个buffer里?
字符串如下:24 00 12 00 1A
我想用来把这个字符串转化为二进制写在binary类型的注册表值中?
其他部分代码如下:
          case datatype of
            rdstring:begin
                       reg.WriteString(value,data);
                       result:=true;
                     end;
            rdBinary:begin
                       mybuffersize:=length(data)/2;
                       GetMem(mybuffer,mybuffersize);
                       for j:=0 to mybuffersize -1 do
                       begin
                         hextobin();
                       end;
                       reg.WriteBinaryData(value,mybuffer,mybuffersize);
                       result:=true;
                     end;
            rdinteger:begin
                        reg.WriteInteger(value,strtoint(data));
                        result:=true;
                      end;

解决方案 »

  1.   

    //一条语句就可以了~~
    WriteBinaryData('', mybuffer[1], Length(mybuffer));
      

  2.   

    //那就写一对函数吧~~
    function WriteValue(mRootKey: HKEY; mKey, mName, mData: string): Boolean;
    var
      vRegDataInfo: TRegDataInfo;
    begin
      Result := False;
      with TRegistry.Create do try
        RootKey := mRootKey;
        if not OpenKey(mKey, False) then Exit;
        GetDataInfo(mName, vRegDataInfo);
        case vRegDataInfo.RegData of
          rdUnknown: Exit;
          rdString, rdExpandString: WriteString(mName, mData);
          rdInteger: WriteInteger(mName, StrToIntDef(mData, 0));
          rdBinary: WriteBinaryData(mName, mData[1], Length(mData));
        end;
      finally
        CloseKey;
        Free;
      end;
      Result := True;
    end; { WriteValue }
      

  3.   

    这是以前写的代码,供参考,字符串中不允许有空格。array of byte可以用在需要buffer类型参数的程序中
    type
      BufType :array of byte;
    function MakeBuffer(src :string) :BufType;
    var
      buf :BufType;
      i,j :integer;
    begin
      j := length(src) div 2;  
      setlength(buf,j);
      for i := 0 to j-1 do
      begin
        buf[i] := strtoint('$'+midstr(src,i*2+1,2));
      end;
      Result := buf;
    end;
      

  4.   

    老大们:
       照你们这样是吧字符串的ascii码写进去了,我要把这个字符串转化为16进制呀!
      

  5.   

    你仔细看看我的代码,已经转换成16进制,这样调用
    var buff :array of byte;
    buff := makebuffer(data)
    rdBinary: WriteBinaryData(mName, buff , Length(buff));