realbasic-nug
[Top] [All Lists]

Re: Memoryblock question

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Memoryblock question
From: Phil Mobley <phil at mobleybros dot com>
Date: Thu, 30 Dec 2004 21:50:28 -0800
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <3506ECBC-5ACF-11D9-B88E-000D9332E452 at netbox dot com>
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>

<Prev in Thread] Current Thread [Next in Thread>