Page 1 of 1

Convert

PostPosted: Wed Sep 06, 2023 12:24 pm
by Natter
Hi,

I need to convert a block of code ({||}) to a string. How can I do this?

Re: Convert

PostPosted: Wed Sep 06, 2023 1:30 pm
by nageswaragunupudi
If the purpose is to save the string and restore later for execution:
Code: Select all  Expand view
  b := { || MsgInfo( "Good" ) }
   Eval( b )
   c := HB_Serialize( b )
   ? ValType( c ), Len( c ), STRTOHEX( c )

   // save the string as hex and restore

   b := HB_DeSerialize( c )
   ? Eval( b )
 


But, this works with xHarbour but not with Harbour.
With Harbour, no errors but HB_Serialize() returns a NULL string.

Re: Convert

PostPosted: Wed Sep 06, 2023 2:02 pm
by karinha

Re: Convert

PostPosted: Wed Sep 06, 2023 2:13 pm
by Natter
Thanks !

Re: Convert

PostPosted: Wed Sep 06, 2023 3:15 pm
by nageswaragunupudi
ValToPrg() and ValToPrgExp() are not useful for Objects and Codeblocks.

Re: Convert

PostPosted: Wed Sep 06, 2023 6:39 pm
by Otto
Dear Rao,
Just out of curiosity, I haven't really delved into the question yet. But would JSON encode and decode also be an option?
Best regards,
Otto

Re: Convert

PostPosted: Thu Sep 07, 2023 5:12 am
by nageswaragunupudi
Otto wrote:Dear Rao,
Just out of curiosity, I haven't really delved into the question yet. But would JSON encode and decode also be an option?
Best regards,
Otto

Even to encode into Json, we first need to convert the codeblock into a "text" string.
How?
The only function available is HB_Serialize( bBlock ) --> cBinaryString and then convert into text using STRTOHEX(), before we encode it as Json or directly save into a field.
Unfortunately, this function is working in xHarbour only and not in Harbour.
Even there the limitation is that the codeblock should not have any references to local/static variables and static functions.

Re: Convert

PostPosted: Thu Sep 07, 2023 6:46 am
by hua
I used cMimeEnc() instead of strtohex()