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

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as