Xojo Conferences
XDCMay2019MiamiUSA

MemoryBlock question (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Drawer Questions
Next thread: Extends bug?


Win32 API Declares   -   Berg, Heath
  MemoryBlock question   -   Scott
   Re: MemoryBlock question   -   Brady Duga
   Memoryblock question   -   Ken Fleisher
   Re: Memoryblock question   -   Charles Yeomans
   Re: Memoryblock question   -   Will Leshner
   Re: Memoryblock question   -   Phil Mobley
   Re: Memoryblock question   -   Ken Fleisher
   Re: Memoryblock question   -   Stuart Malin
    Re: Memoryblock question   -   Ken Fleisher
     Re: Memoryblock question   -   Phil Mobley
   Re: Memoryblock question   -   Charles Yeomans
    Re: Memoryblock question   -   Ken Fleisher
     Re: Memoryblock question   -   Phil Mobley

MemoryBlock question
Date: 30.01.05 03:15 (Sat, 29 Jan 2005 20:15:56 -0600)
From: Scott
I have a situation where I need to DIM a memory block and pass it to a
function that assigns it a string value. Essentially I'm making a function
to create a pointer to a string in memory. I can do this inside a function
but passing an unassigned memory bock has me stymied. Any ideas?


-Scott.


Webconnected International

Killeen, Texas USA

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: MemoryBlock question
Date: 30.01.05 03:35 (Sat, 29 Jan 2005 18:35:02 -0800)
From: Brady Duga

On Jan 29, 2005, at 6:15 PM, Scott wrote:

> I have a situation where I need to DIM a memory block and pass it to a
> function that assigns it a string value. Essentially I'm making a
> function
> to create a pointer to a string in memory. I can do this inside a
> function
> but passing an unassigned memory bock has me stymied. Any ideas?

Just pass it like any other object. But I'm not clear what you are
trying to do - wouldn't something like this work:

dim mb as MemoryBlock
dim s as string

s = "abc"
mb = s

Or do you need to do something else? If so, you might create the
MemoryBlock in the function and return it:

Function Stringify(aString as string) as MemoryBlock
dim mb as MemoryBlock

<do something fancy here, so mb has a string in it>
return mb
end function

then:

dim mb as MemoryBlock
mb = Stringify("abc")

--Brady
The La Jolla Underground

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Memoryblock question
Date: 31.12.04 00:23 (Thu, 30 Dec 2004 18:23:47 -0500)
From: Ken Fleisher
Hi. I know this is a simple question, but I just have not been able to
figure it out. I have data in a memoryblock and I want to put a portion
of it into a string variable as hex code. I've been trying to use the
following to transfer the data:

var = m.stringvalue( offset, size )

and variations on this. What I can't figure out is how to get the hex
representation of the data. The data is not actually a string, so is
there a different way to transfer an arbitrary block of data and store
it as hex?

Thanks!

Ken Fleisher

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 01:16 (Thu, 30 Dec 2004 19:16:15 -0500)
From: Charles Yeomans

On Dec 30, 2004, at 6:23 PM, Ken Fleisher wrote:

> Hi. I know this is a simple question, but I just have not been able to
> figure it out. I have data in a memoryblock and I want to put a
> portion of it into a string variable as hex code. I've been trying to
> use the following to transfer the data:
>
> var = m.stringvalue( offset, size )
>
> and variations on this. What I can't figure out is how to get the hex
> representation of the data. The data is not actually a string, so is
> there a different way to transfer an arbitrary block of data and store
> it as hex?
>

Here's one way:

For i = start to finish
s = s + Hex(m.Byte(i))
Next

--------------
Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 01:17 (Thu, 30 Dec 2004 16:17:46 -0800)
From: Will Leshner
On Thu, 30 Dec 2004 18:23:47 -0500, Ken Fleisher <<email address removed>> wrote:

> The data is not actually a string, so is
> there a different way to transfer an arbitrary block of data and store
> it as hex?

I don't think there's much magic to rely on here. What form is the
data? Would the Hex function help you at all?

Re: Memoryblock question
Date: 31.12.04 01:23 (Thu, 30 Dec 2004 16:23:36 -0800)
From: Phil Mobley
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 02:02 (Thu, 30 Dec 2004 20:02:08 -0500)
From: Ken Fleisher
Thanks for the suggestions. I'll give the functions a try.

Related question--what about if I just want to store an arbitrary block
from the memoryblock as a bit for bit binary copy in a variable? What's
the best way to do copy it over and what type of variable would you
suggest? (this is why I was using a string, but it may not be the best
choice.)

Someone had asked what the data is--I am parsing the lookup tables and
matrices in ICC profiles and displaying the information (basically like
the Colorsync Utility on OS X does). I also have to make it so that any
of the data can be viewed in hex, which is the source of my questions.

Thanks!

Ken

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 02:23 (Thu, 30 Dec 2004 17:23:41 -0800)
From: Stuart Malin

On Dec 30, 2004, at 4:16 PM, Charles Yeomans wrote:
> On Dec 30, 2004, at 6:23 PM, Ken Fleisher wrote:
>
>> Hi. I know this is a simple question, but I just have not been able
>> to figure it out. I have data in a memoryblock and I want to put a
>> portion of it into a string variable as hex code. I've been trying to
>> use the following to transfer the data:
>>
>> var = m.stringvalue( offset, size )
>>
>> and variations on this. What I can't figure out is how to get the hex
>> representation of the data. The data is not actually a string, so is
>> there a different way to transfer an arbitrary block of data and
>> store it as hex?
>>
> Here's one way:
>
> For i = start to finish
> s = s + Hex(m.Byte(i))
> Next

This example makes the point of the need to "hexify" the characters,
but, given other threads on the immutableness of strings, I now cringe
when I see:
s = s + .....
in a For...Next loop.

I saw Phil's posting, so know that a more efficient implementation has
already been provided.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 02:56 (Thu, 30 Dec 2004 20:56:37 -0500)
From: Ken Fleisher
Phil, I tried your HexLower function and I had two problems so far:

1) sourceB = source // automatic conversion to memoryblock

This line gives me a type mismatch error.

2) Case 0 to 9

This line gives a syntax error.

(I am using RB v5.2.4 on a Mac OS X v10.3.6)

Ken

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 06:50 (Thu, 30 Dec 2004 21:50:28 -0800)
From: Phil Mobley
On Dec 30, 2004, at 5:56 PM, Ken Fleisher wrote:

> Phil, I tried your HexLower function and I had two problems so far:
>
> 1) sourceB = source // automatic conversion to memoryblock
>
> This line gives me a type mismatch error.
>
> 2) Case 0 to 9
>
> This line gives a syntax error.
>
> (I am using RB v5.2.4 on a Mac OS X v10.3.6)

Oh.

I only use 5.5.x.

For the first line, you need to do this instead:

sourceB = NewMemoryBlock(LenB(source))
sourceB.StringValue(0, sourceB.Size) = source

For the second, I would do it as an If...Then...Else, so use this
instead:

If r < 10 Then
resultB.Byte((k*2)+1) = r + 48
Else
resultB.Byte((k*2)+1) = r + 87
End If

Here is the whole thing rewritten:

Function HexLower(source As String) As String

dim sourceB, resultB as MemoryBlock
dim k, r, size as Integer

sourceB = NewMemoryBlock(LenB(source))
sourceB.StringValue(0, sourceB.Size) = source
resultB = NewMemoryBlock(sourceB.Size * 2)
size = sourceB.Size - 1

For k = 0 To size
r = Bitwise.BitAnd(sourceB.Byte(k), &hF)
If r < 10 Then
resultB.Byte((k*2)+1) = r + 48
Else
resultB.Byte((k*2)+1) = r + 87
End If

r = Bitwise.BitAnd(Bitwise.ShiftRight(sourceB.Byte(k), 4), &hF)
If r < 10 Then
resultB.Byte(k*2) = r + 48
Else
resultB.Byte(k*2) = r + 87
End If
Next

Return resultB.StringValue(0, resultB.Size)

End Function

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 17:20 (Fri, 31 Dec 2004 11:20:20 -0500)
From: Charles Yeomans

On Dec 30, 2004, at 8:23 PM, Stuart Malin wrote:

>
> On Dec 30, 2004, at 4:16 PM, Charles Yeomans wrote:
>> On Dec 30, 2004, at 6:23 PM, Ken Fleisher wrote:
>>
>>> Hi. I know this is a simple question, but I just have not been able
>>> to figure it out. I have data in a memoryblock and I want to put a
>>> portion of it into a string variable as hex code. I've been trying
>>> to use the following to transfer the data:
>>>
>>> var = m.stringvalue( offset, size )
>>>
>>> and variations on this. What I can't figure out is how to get the
>>> hex representation of the data. The data is not actually a string,
>>> so is there a different way to transfer an arbitrary block of data
>>> and store it as hex?
>>>
>>>
>> Here's one way:
>>
>> For i = start to finish
>> s = s + Hex(m.Byte(i))
>> Next
>
> This example makes the point of the need to "hexify" the characters,
> but, given other threads on the immutableness of strings, I now cringe
> when I see:
> s = s + .....
> in a For...Next loop.

Don't. For a small (<1000 or so) iterations, using String
concatenation is usually good enough.

--------------
Charles Yeomans

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 31.12.04 17:25 (Fri, 31 Dec 2004 11:25:58 -0500)
From: Ken Fleisher

Thanks Phil. I have already put in the code that Charles had suggested
and it works fine for this purpose, but I will try out your routine and
hold onto it as well. I appreciate it!

Ken

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Re: Memoryblock question
Date: 01.01.05 06:49 (Fri, 31 Dec 2004 21:49:09 -0800)
From: Phil Mobley
On Dec 31, 2004, at 8:25 AM, Ken Fleisher wrote:

> Thanks Phil. I have already put in the code that Charles had suggested
> and it works fine for this purpose, but I will try out your routine
> and hold onto it as well. I appreciate it!

That code has one flaw. The Hex() function will strip out leading
zeros, so any values less than 15 will be incorrect -- this was the
whole reason why I wrote the other function.

For example, if you have a block of data like this:

F908314C380001AC7E02

Would output like this:

F98314C301AC7E2

So at the minimum, you would need to change the function to:

For i = start to finish
If m.Byte(i) < 16 Then
s = s + "0" + Hex(m.Byte(i))
Else
s = s + Hex(m.Byte(i))
End If
Next

It has been a while since I timed the difference, but I think my
function was about 50% faster than what you see above. Just because
the code is simpler does not mean that it is more efficient. But like
Charles said, you will not be able to notice the difference for less
than a 1000 bytes.

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>