Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert structure to string

491 views
Skip to first unread message

Wim Simons

unread,
Sep 1, 2009, 5:10:22 AM9/1/09
to
Powerbuilder 11.5.1 build 4011

Dear all,

Anyone knows how a structure can be converted to a string?
We are using structures containing char array fields and want converts these
to a string.

ex.
struct x
char field1[50]
char field2[50]

We wuld like to convert this on a generic way to a string (100 chars). The
structure can contain different fields, we are looking for a generic way to
do the job using powerbuilder 11.5.

Any ideas?

Best regards,
Wim Simons


Jeremy Lakeman

unread,
Sep 1, 2009, 6:00:26 AM9/1/09
to

Assuming you mean the structure will only contain char's and no
zero's. You can mis-configure an external function;

subroutine CopyStructure (ref string dest, ref structure source, long
length) library "kernel32.dll" alias for "RtlMoveMemory"

Then make sure you preallocate the string and you'll need to know the
number of bytes to copy;

ls_dest = space(100)
CopyStructure(ls_dest,x,100)

Wim Simons

unread,
Sep 1, 2009, 8:16:02 AM9/1/09
to
Dear Jeremy,

We are using the RtlMoveMemory function, but that is crashing a lot.
In debug it gives strange results, seems like references of the string and
the structure are messing up.
I was rather looking for an alternative for the RtlMoveMemory function.

Best regards,
Wim Simons

"Jeremy Lakeman" <jeremy....@gmail.com> schreef in bericht
news:666a326e-b768-405d...@g1g2000pra.googlegroups.com...

Scott Morris

unread,
Sep 1, 2009, 8:27:28 AM9/1/09
to
"Wim Simons" <in...@simconsult.be> wrote in message
news:4a9ce4fe@forums-1-dub...

> Powerbuilder 11.5.1 build 4011
>
> Dear all,
>
> Anyone knows how a structure can be converted to a string?
> We are using structures containing char array fields and want converts
> these to a string.
>
> ex.
> struct x
> char field1[50]
> char field2[50]
>
> We wuld like to convert this on a generic way to a string (100 chars).
> The

So how do you intend to mark the end of each individual string within the
larger converted string?

> structure can contain different fields, we are looking for a generic way
> to do the job using powerbuilder 11.5.

Convert your structure to a class. Expose the structure some how - perhaps
as a public instance var or via an "access" method (problem - structure
definitions have scope). Create a method to do your conversion. With an
appropriate class hierarchy, you should be able to do this in a generic
manner.

Whether that solves your problem I can't say since I don't know what you
actually intend to do with the converted value.


Jeremy Lakeman

unread,
Sep 1, 2009, 10:26:03 AM9/1/09
to
On Sep 1, 9:16 pm, "Wim Simons" <i...@simconsult.be> wrote:
> Dear Jeremy,
>
> We are using the RtlMoveMemory function, but that is crashing a lot.
> In debug it gives strange results, seems like references of the string and
> the structure are messing up.
> I was rather looking for an alternative for the RtlMoveMemory function.
>
> Best regards,
> Wim Simons
>
> "Jeremy Lakeman" <jeremy.lake...@gmail.com> schreef in berichtnews:666a326e-b768-405d...@g1g2000pra.googlegroups.com...

weeeelll, I'm not sure why this is crashing for you, but you basically
have 2 other options.
1, inherit all your "structures" from a common autoinstantiate nvo
with a to_string() method that is implemented in each descendant.
2, copy the structure into a datawindow row using dot notation. Not
sure if you can get away with a single dataobject with max(N) string
columns (or perhaps N rows? that might work...)
eg;
ds_1.object.data[1]=lstr_struct
ls_ret = ds_1.object.datawindow.data

or maybe...
ds_1.object.string_column.primary=lstr_struct
(but that's a long shot I haven't investigated before, converting from
a structure may only work for rows...)

Ivaylo Ivanov

unread,
Sep 1, 2009, 10:40:17 AM9/1/09
to
I would choose the same approach - to create an autoinstantiate NVO with
instance variables char field1[50] char field2[50] and implement a
serialization method for your needs... if the usage of structure in your
case is not a strong requirement.

"Scott Morris" <bo...@bogus.com> wrote in message
news:4a9d1330$1@forums-1-dub...

Peter Piechutzki

unread,
Sep 9, 2009, 9:26:17 AM9/9/09
to
Hi Wim,

maybe I'm missing something, I don't actually see the point in calling
external functions at all.
How about just implementing a PB function:

global function string f_char2string (character field[]);
long ll_count
string ls_text
ls_text="" // Initialize to empty String (not NULL!)
for ll_count=1 to upperbound(field)
if not isnull(field[ll_count]) then ls_text += string(field[ll_count])
next
return ls_text
end function

following scipt as example using above function:

str_tester lstr_test
string ls_text
long ll_count
// Fill first structure char array with every second char set to "A" and
second char array with every secon char set to "B"
for ll_count=1 to 50 step 2
lstr_test.field1[ll_count]="A"
lstr_test.field2[ll_count]="B"
next


// Won'nt work, PB cuts off att NULL Char
ls_text = lstr_test.field1 + lstr_test.field2
messagebox("Text",ls_text)

// Will work, PB ignores NULL Char
ls_text=f_char2string(lstr_test.field1)
ls_text+=f_char2string(lstr_test.field2)
messagebox("Text",ls_text)


IF your string is allways to represent the same length of both structure
char array's you'ld have to modify the f_char2string and decide how to
replace the NULL chars . PB assumes NULL in a string as the terminator of
the string.

You could adopt the above function to accept a structure, if that vary's in
number of fields and types the whole would be a little more complicated. But
even then I don't see the necessarity of using external program calls.
Regards

Peter P

"Wim Simons" <in...@simconsult.be> schrieb im Newsbeitrag
news:4a9ce4fe@forums-1-dub...

0 new messages