Link to home
Start Free TrialLog in
Avatar of Vector7
Vector7Flag for United Kingdom of Great Britain and Northern Ireland

asked on

PByte to PAnsiChar Conversion

Hi,
I'm migrating a system from Delphi 7 to Delphi XE4 and running into all sorts of conversion problems.
Is there a conversion function for converting PByte to PAnsiChar?
Many thanks
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

you could just typecast it e.g. PAnsiChar(some_byte_var)

or you could do something like:


var b: byte;
  pb: pbyte;
  pc: PAnsiChar;
begin
  b:= 65;
  pb:= @b; //pb^ will be 65
  pc:= @b;  //pc^ will be 'A'
...
Avatar of Vector7

ASKER

Hi,

Thanks for your reply.  The problem is that only the first character of 'Value' (code below) is being saved in the statement e.g. a 'Value' of 2048 is saved as 2.

          Check(DbiCfgModifyRecord(hCfg, P, ParamCount, pFld, pRec));


Herewith the code:-



var hCfg: hDBICfg;
    Config: SYSConfig;
    Path, Option: string;
    ParamCount, i: word;
    pFields, pFld: pFLDDesc;
    pRecBuf, pRec: pBYTE;
    Found, SelfInitialized: Boolean;
    rslt: DBIResult;
    P: PAnsiChar;
 


   pFields := AllocMem(ParamCount * sizeof(FLDDesc));
     pFld := pFields;
     pRecBuf := AllocMem(10000);
     pRec := pRecBuf;

     {Get the node values... }
     Check(DbiCfgGetRecord(hCfg, StrToPAnsiChar(Path), ParamCount, pFields, pRecBuf));

     for i := 0 to ParamCount - 1 do
        begin
        if pFields^.szName = Option then
           begin
           StrPCopy(PAnsiChar(pRecBuf), Value);
           P := StrToPAnsiChar(Path);
           Check(DbiCfgModifyRecord(hCfg, P, ParamCount, pFld, pRec));
           Found := True;
           end;

        Inc(pFields);
        Inc(pRecBuf, 128);
        end;
It seems to me your original question is not exactly what you are after. Your question was if and how to convert a pbyte to pAnsiChar - which I believe I answered.

Just remember that a PByte or a PAnsichar are merely pointers. PByte points to an address and if you dereference it, it will give you the byte value at that address. The same goes for PAnsichar i.e. if you dereference it it will give you the AnsiChar at that address. Regarding your value of 2048, this can't be stored in a single byte (max is 255).

From your code, it looks like DbiCfgGetRecord returns a pointer to the fields (pFields) and its values as a buffer (pRecBuf).

Looking at your loop through the paramcount it seems that the buffer allocates 128 bytes for each field/parameter value (see: Inc(pRecBuf, 128); ).

It looks like what you are trying to do is set the pRecBuf (the parameter value buffer) to contain the value you pass, is this right? What is "Value" defined as? this may be your problem. try: StrPCopy(PAnsiChar(pRecBuf), AnsiString(Value));
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why a b grade?
Thank you