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 FunctionEnd Class
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job