Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB7
Expertise: Intermediate
Dec 2, 2002

MK? And CV? - Convert numbers to strings and back

The following routines convert a numeric value into a string that represents the number, and vice versa. They are useful for reading data written by QuickBasic programs, because the QuickBasic language functions that did the conversions were not ported to Visual Basic.

Points to note:
1. These routines are a .NET update to the VB5/VB6 Code Bank article MK? And CV? - Convert numbers to strings and back.
2. The data type differences between VB.NET and previous versions of VB and QB, have forced a change to the parameters of the function calls and API calls - Long (QB/VB) = Integer (.NET), Integer (QB/VB) = Short (.NET), etc.
3. The class calls "unmanaged" code via the API calls. Therefore there may be permission problems when trying to install and run Internet applications that use this code in a .NET environment.
4. Because .NET doesn't support the Currency data type and the Variant data type, the VB5/VB6 routines in the original Code Bank article that handled those data types were not ported.


Option Explicit On 
Imports System.Runtime.InteropServices

'Important: This class calls "unmanaged" code.
Public Class QB2VB
    'Note: The data types 'Currency' and 'Variant' are not supported

    'Type-safe declarations because 'As Any' is not supported in .NET
    Private Declare Sub CopyMemoryMKD Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByVal hDest As String, ByRef hSource As Double, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryCVD Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByRef hDest As Double, ByVal hSource As String, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryMKS Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByVal hDest As String, ByRef hSource As Single, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryCVS Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByRef hDest As Single, ByVal hSource As String, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryMKL Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByVal hDest As String, ByRef hSource As Integer, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryCVL Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByRef hDest As Integer, ByVal hSource As String, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryMKI Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByVal hDest As String, ByRef hSource As Short, ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryCVI Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByRef hDest As Short, ByVal hSource As String, ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryMKDt Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByVal hDest As String, ByRef hSource As Double, _
        ByVal iBytes As Integer)
    Private Declare Sub CopyMemoryCVDt Lib "Kernel32" Alias "RtlMoveMemory" _
        (ByRef hDest As Double, ByVal hSource As String, _
        ByVal iBytes As Integer)

    'Convert from Double to String.
    Shared Function MKD(ByRef Value As Double) As String
        Dim sTemp As String = Space(8)
        CopyMemoryMKD(sTemp, Value, 8I)
        Return sTemp
    End Function

    'Convert from String to Double.
    Shared Function CVD(ByRef Argument As String) As Double
        Dim dTemp As Double = 0.0R
        If Len(Argument) <> 8 Then
            Return Double.NaN
        End If
        CopyMemoryCVD(dTemp, Argument, 8I)
        Return dTemp
    End Function

    'Convert from Single to String.
    Shared Function MKS(ByRef Value As Single) As String
        Dim sTemp As String = Space(4)
        CopyMemoryMKS(sTemp, Value, 4I)
        Return sTemp
    End Function

    'Convert from String to Single.
    Shared Function CVS(ByRef Argument As String) As Single
        Dim sTemp As Single = 0.0F
        If Len(Argument) <> 4 Then
            Return Single.NaN
        End If
        CopyMemoryCVS(sTemp, Argument, 4I)
        Return sTemp
    End Function

    'Convert from (QB)Long to String.
    'QB/VB Long (4 bytes) => .NET Integer (Int32)
    Shared Function MKL(ByRef Value As Integer) As String
        Dim sTemp As String = Space(4)
        CopyMemoryMKL(sTemp, Value, 4I)
        Return sTemp
    End Function

    'Convert from String to (QB)Long.
    'QB/VB Long (4 bytes) => .NET Integer (Int32)
    Shared Function CVL(ByRef Argument As String) As Long
        Dim lTemp As Integer = 0I
        If Len(Argument) <> 4 Then
            Return Long.MinValue
        End If
        CopyMemoryCVL(lTemp, Argument, 4I)
        Return CLng(lTemp)  'Cast Integer into Long
    End Function

    'Convert from (QB)Integer to String.
    'QB/VB Integer (2 bytes) => .NET Short (Int16)
    Shared Function MKI(ByRef Value As Short) As String
        Dim sTemp As String = Space(2)
        CopyMemoryMKI(sTemp, Value, 2I)
        Return sTemp
    End Function

    'Convert from String to (QB)Integer.
    'QB/VB Integer (2 bytes) => .NET Short (Int16)
    Shared Function CVI(ByRef Argument As String) As Integer
        Dim iTemp As Short = 0S
        If Len(Argument) <> 2 Then
            Return Integer.MinValue
        End If
        CopyMemoryCVI(iTemp, Argument, 2I)
        Return CInt(iTemp)  'Cast Short into Integer
    End Function

    'Convert from Date (OLE Automation-compatible [Double]) to String
    Shared Function MKDt(ByRef Value As Double) As String
        Dim sTemp As String = Space(8)
        CopyMemoryMKDt(sTemp, Value, 8I)
        Return sTemp
    End Function

    'Convert from String to Date
    Shared Function CVDt(ByRef Argument As String) As DateTime
        Dim dTemp As Double = 0.0R
        If Len(Argument) <> 8 Then
            Return DateTime.MinValue
        End If
        CopyMemoryCVDt(dTemp, Argument, 8I)
        Return Date.FromOADate(dTemp)   'Cast Double into Date
    End Function

End Class
Adam Phillips
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap