On Dec 30, 2004, at 3: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?
The function below will convert any string to it's hex equivalent.
This is one that I wrote to convert RB Md5 hashes to something to be
used in PHP, so the A - F values are represented as lower case (you can
change this if you need to).
It is very fast, *much* faster than trying to read strings directly.
It is also *significantly* faster than using the built in Hex()
function because you are not creating a bunch of strings and putting
them together. Another weakness of the built-in function is that it
will ignore padding '0' (such as 0F060407 would look like F647 if you
looped through each character).
Joe also has a different version in the StringUtil module (which is
very useful).
----
Function HexLower(source As String) As String
dim sourceB, resultB as MemoryBlock
dim k, r, size as Integer
sourceB = source // automatic conversion to memoryblock
resultB = NewMemoryBlock(sourceB.Size * 2)
size = sourceB.Size - 1
For k = 0 To size
r = Bitwise.BitAnd(sourceB.Byte(k), &hF)
Select Case r
Case 0 to 9
resultB.Byte((k*2)+1) = r + 48
Case 10 to 15
resultB.Byte((k*2)+1) = r + 87
End Select
r = Bitwise.BitAnd(Bitwise.ShiftRight(sourceB.Byte(k), 4), &hF)
Select case r
Case 0 to 9
resultB.Byte(k*2) = r + 48
Case 10 to 15
resultB.Byte(k*2) = r + 87
End Select
Next
Return resultB // automatic conversion to string
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>
|