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

PB example for BlobMid() , BlobEdit() (Urgent)

1,263 views
Skip to first unread message

Jenny

unread,
Dec 1, 2009, 7:50:40 AM12/1/09
to
Hi All,

I am new to PowerBuilder.
I am using PB 11, can anyone give me very small PB example
which demonstrate BlobMid() , BlobEdit() ( not event) of
PowerBuilder.


Thanks
Jenny

Ivaylo Ivanov

unread,
Dec 1, 2009, 9:21:33 AM12/1/09
to
function f_hex_to_blob(string as_input) returns blob
==================================
// Purpose: converts a hex string to a blob of the corresponding bytes
// Arguments: string as_input - the string to be converted (with HEX
digits!)
// Returns: blob - the byte contents

blob lblb_result
string ls_hex = "0123456789ABCDEF", ls_temp
long ll_index
byte lbt_temp

if mod(len(as_input), 2) = 1 then
as_input = "0" + as_input
end if

// The working blob is created with 1 additional byte,
// because when BlobEdit function is called for the last byte
// of the blob, it returns NULL (position at which the next data
// can be copied - not enough space) and doesn't perform
// the operation for the last byte of the blob!

lblb_result = Blob(space(len(as_input) / 2 + 1), EncodingANSI!)

for ll_index = 1 to len(as_input) step 2
// Get next byte from the string
ls_temp = Upper(mid(as_input, ll_index, 2))
lbt_temp = 16 * (pos(ls_hex, mid(ls_temp, 1, 1)) - 1) + pos(ls_hex,
mid(ls_temp, 2, 1)) - 1

// Put the byte into the blob
BlobEdit(lblb_result, (ll_index + 1)/ 2, lbt_temp)
next

lblb_result = BlobMid(lblb_result, 1, len(lblb_result) - 1)

return lblb_result


Is this enough? If not, have you looked at the examples in the help for
these functions?

Regards,
Ivaylo

<Jenny> wrote in message news:4b151120.490...@sybase.com...

Jenny

unread,
Dec 2, 2009, 6:26:41 AM12/2/09
to
why i can't add two strings with BlobEdit(), I tried below
code but i am not able to get proper result ...Date + Day as
a one string


-----------------------Code ---------------------------
blob{100} bb

long pos1, pos2

string test_day

date test_date

test_date = Today()

test_day = DayName(test_date)

// Store data in the blob

pos1 = BlobEdit( bb, 1, string(test_date))

messagebox("Position for Date", string(pos1))

pos2 = BlobEdit( bb, pos1, test_day )

// Extract the status stored in bb and display it

messagebox("Position for Day", string(pos2))


messagebox("I am expecting result should be ‘Date +
Day’, why I am getting only Date?", String( BlobMid(bb,
1, len(bb))))

string ls_s
ls_s = String( BlobMid(bb, pos1, pos2 - pos1))

messagebox("ls_s", ls_s)

-----------------------Code ---------------------------

Jenny

unread,
Dec 2, 2009, 6:29:06 AM12/2/09
to

Ivaylo Ivanov

unread,
Dec 2, 2009, 7:20:40 AM12/2/09
to
Well, Jenny, I think your problem comes from the way PB stores strings in
the blob using BlobEdit. Read the comments in the help of the function. It
would add 2 zero bytes which is the NULL terminating character after the
real contents of the first string (your "Date" part). Second BlobEdit would
then add your "Day" contents as a string and again 00 00 after it.

So if you try to convert the entire blob contents back to string, the
conversion will stop on the first null terminating character thus returning
just the "Date" part. So you would ask for the solutions and here they are:

1) A general solution to BLOB-related problems is to have a convenient way
to observe what's really happening with their values by saving the contents
of a blob variable to a binary file. Here is a sample script (sorry, no
error-handling!) which you can add somewhere at the end or anywhere between
the statements of your script to make such export:

integer li_FileNum
li_FileNum = FileOpen("test.blb", StreamMode!, Write!, LockReadWrite!,
Replace!)
FileWriteEx(li_FileNum, bb)
FileClose(li_FileNum)

Run your script, open the binary file in any hex viewer/editor and ....
there you would see...

2) Of course, now as you know the problem, first workaround would be to
decrease pos1 by 2 in the second call like this:

pos2 = BlobEdit( bb, pos1 - 2, test_day )

3) second workaround (which I prefer) would be to concatenate the two
strings and put their concatenation in the blob with just one BlobEdit:

pos1 = BlobEdit(bb, 1, string(test_date) + test_day)

... or even not using BlobEdit, but a direct Blob() call:

bb = Blob(string(test_date) + test_day)

So always have in mind that blob-->string conversion would stop on the 00 00
(null) terminating sequence.

BTW, this "String(BlobMid(bb,1, len(bb)))" can be just "String(bb)".

Regards,
Ivaylo

<Jenny> wrote in message news:4b164ef0.687...@sybase.com...

Jenny

unread,
Dec 2, 2009, 8:42:33 AM12/2/09
to
1. when i use string value in BlobEdit() then it is
overwriting the old sting value
2. When I am trying to get the date value my BlobMid() then
I am getting garbage, can to tell me

How I can achieve both the things…

Thanks,
Jenny

Ivaylo Ivanov

unread,
Dec 2, 2009, 9:46:39 AM12/2/09
to
It's all in my answer: explore the exact contents of the blob in each of
your steps. Use BlobMid with exact parameter values to extract just the
bytes corresponding to your "Date" part and then pass the sub-blob to
String() function.

Also, when you're observing the byte contents of the blob take into account
the 2-byte representation UTF16LE! which is the default of PB10+.

If you post a newer code here, I would probably help you further with what
you're trying to achieve, but you should describe the exact problem.

Regards,
Ivaylo

<Jenny> wrote in message news:4b166ec9.6d5...@sybase.com...


1. when i use string value in BlobEdit() then it is
overwriting the old sting value
2. When I am trying to get the date value my BlobMid() then
I am getting garbage, can to tell me

How I can achieve both the things.

Scott Morris

unread,
Dec 2, 2009, 11:39:14 AM12/2/09
to
The behavior of blobedit is affected by the datatype of the data you pass to
it. There is additional information in the online help. This type of code
requires that you are very familiar with the internal representation of
data - especially strings, their null termination characters, and
differences between ansi/unicode. Below is a trivial change to your code
that does you want.


blob{100} bb
long pos1, pos2

string test_day

date test_date

test_date = Today()

test_day = DayName(test_date)

// Store data in the blob

// NOTICE THE CHANGE HERE
pos1 = BlobEdit( bb, 1, blob(string(test_date)))

messagebox("Position for Date", string(pos1))

pos2 = BlobEdit( bb, pos1, test_day )

// Extract the status stored in bb and display it

messagebox("Position for Day", string(pos2))

messagebox("I am expecting result should be 'Date Day', why I am getting
only Date?", &
String (BlobMid (bb, 1, len(bb))))
return 1


Roland Smith [TeamSybase]

unread,
Dec 3, 2009, 11:09:56 PM12/3/09
to
Why are you combining strings into a blob in the first place?

When using BlobEdit the blob variable must already contain data that you are
changing. It is not for initial population of the blob. Try this:

Blob lblob_data
ULong lul_rtn
String ls_data

ls_data = "something"

lblob_data = Blob(Space(100))

lul_rtn = BlobEdit(lblob_data, 10, ls_data)


This example will first populate the blob variable with 100 spaces. Then it
takes the value in ls_data and updates the blob starting with position 10.


<Jenny> wrote in message news:4b166ec9.6d5...@sybase.com...


1. when i use string value in BlobEdit() then it is
overwriting the old sting value
2. When I am trying to get the date value my BlobMid() then
I am getting garbage, can to tell me

How I can achieve both the things.

Roland Smith [TeamSybase]

unread,
Dec 3, 2009, 11:15:22 PM12/3/09
to
If you were to read the help file for BlobEdit you would see that the
function can only be used on blob variables that already contain data. If
the blob variable is empty then the function fails.

Are you just messing around trying to learn PowerBuilder or does this relate
to a specific business requirement?

It is usually a good idea to explain why you want to do something. Someone
may come up with a completely different way of doing something. A narrowly
worded question wouldn't get that response.


<Jenny> wrote in message news:4b164f7b.688...@sybase.com...

Jeremy Lakeman

unread,
Dec 4, 2009, 5:13:49 AM12/4/09
to
blobedit is *much* faster than string+= for building big strings from
little pieces.
Then when blobedit returns null you can quickly double the buffer size
to make more room by appending the blob to itself.

On Dec 4, 2:09 pm, "Roland Smith [TeamSybase]"


<topwiz.smith_at_comcast_dot_net> wrote:
> Why are you combining strings into a blob in the first place?
>
> When using BlobEdit the blob variable must already contain data that you are
> changing. It is not for initial population of the blob. Try this:
>
> Blob lblob_data
> ULong lul_rtn
> String ls_data
>
> ls_data = "something"
>
> lblob_data = Blob(Space(100))
>
> lul_rtn = BlobEdit(lblob_data, 10, ls_data)
>
> This example will first populate the blob variable with 100 spaces. Then it
> takes the value in ls_data and updates the blob starting with position 10.
>

> <Jenny> wrote in messagenews:4b166ec9.6d5...@sybase.com...

Roland Smith [TeamSybase]

unread,
Dec 5, 2009, 9:04:06 PM12/5/09
to
That is a good tip.

How about when you first initialize the blob. Is there something faster than
Blob(Space(x))?

"Jeremy Lakeman" <jeremy....@gmail.com> wrote in message
news:230610f0-63fb-438c...@r24g2000prf.googlegroups.com...

Jeremy Lakeman

unread,
Dec 6, 2009, 7:28:10 AM12/6/09
to
new_blob = heres_one_we_prepared_earlier

;)

Useful if you always use blobs that are the same size.

On Dec 6, 12:04 pm, "Roland Smith [TeamSybase]"


<topwiz.smith_at_comcast_dot_net> wrote:
> That is a good tip.
>
> How about when you first initialize the blob. Is there something faster than
> Blob(Space(x))?
>

> "Jeremy Lakeman" <jeremy.lake...@gmail.com> wrote in message

Jenny

unread,
Jan 5, 2010, 9:23:38 AM1/5/10
to
I want to understand the return value of blobedit() of
Powerbuilder
With string i drive formula : return value of blobedit =
((len(string) +1)*2))+1

How I can drive the formula for date, datetime , int, long ,
double etc.. ?
Can anyone tell me what is generic formula for
Blob
Integer or Int
Boolean
LongLong
Byte
Long
Char or character
Real
Date
String
DateTime
Time
Decimal or Dec
UnsignedInteger, UnsignedInt, or UInt
Double
UnsignedLong or ULong

-------------------Code-------------------------------------------
Blob bb
Whatis formula to get return value = BlobEdit( bb, 1,
string_variable)
Whatis formula to get return value = BlobEdit( bb, 1, int_
variable)
Etc…
-------------------Code-------------------------------------------

Mi

unread,
Jan 5, 2010, 9:27:30 AM1/5/10
to
Anyone have some example to demonstrate this clearly,
example given in PB help not helpful to understand
completely.

> > , 1, len(bb))))

Scott Morris

unread,
Jan 5, 2010, 9:42:50 AM1/5/10
to
<Mi> wrote in message news:4b434c52.1ba...@sybase.com...

> Anyone have some example to demonstrate this clearly,
> example given in PB help not helpful to understand
> completely.

I did - a working example based on the OP's original code - in response to
the next post on the same topic by OP. It will help if you read the
documentation very closely and pay special attention to the information
about strings and encoding. If you don't have a good handle on the
differences between unicode and ansi strings, then you should investigate
that aspect first.


0 new messages