Results 1 to 3 of 3

Thread: MKI Function?

  1. #1

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Thumbs down

    In the GOOD old QBasic and QucikBASIC versions (before Microsoft went ActiveX), we had a command called MKI in QB.

    Whatever became of that function? I need to convert an integer value to a two-byte (fixed-length) string and the other way around. I also need to convert a 4-byte (fixed length) string to a Long and the other way around.

    How do I do this?

    Any help will solve my problems digitally hahaha

  2. #2

    Thread Starter
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Talking ANSWER

    SO COOL!

    I answered my own question, haha.

    I have never done that on a message board like this before. But anyway, the problem was getting the four bytes that make up a Long Integer into a 4-byte String, a 4-byte String into a Single, a two-byte String into an Integer, etc.

    I have now come up with the code -- using some Win API -- and if anyone has need for it, I will post it. Be that as it may; converting a string to a byte array and vice versa would be a "piece of cake". The secret is CopyMemory, which takes too long to explain here.

    PLUS ... (not knowing it at the time), I posted this message in the wrong forum.

    Confusion RULES!

    Bottom line is, if you miss the MKI$, MKL$, MKS$, MKD$, CVL, CVI, CVS, and the CVD functions from QuickBasic, then I've written them. They are yours if you ask.

  3. #3
    New Member
    Join Date
    May 2011
    Posts
    1

    Re: ANSWER

    Bottom line is, if you miss the MKI$, MKL$, MKS$, MKD$, CVL, CVI, CVS, and the CVD functions from QuickBasic, then I've written them. They are yours if you ask.[/QUOTE]

    Here is the Answer of MK Functions

    Module mdlMk

    Public Function MKI(ByVal X As Integer) As String 'As String

    Dim D(16) As Byte, V(2) As Byte
    Dim i As Integer, J As Integer, k As Integer
    Dim N As Integer, C As Integer

    If X < 0 Then D(1) = 1 ' Sign of the number

    C = Math.Abs(X) ' Absolute Value
    For i = 0 To 14 ' Set Binary array
    N = 14 - i
    If Fix(C / 2 ^ N) = 1 Then
    D(i + 2) = 1
    C = C - 2 ^ N
    Else : D(i + 2) = 0
    End If
    Next

    'For i = 1 To 16
    ' Debug.Print D(i);
    'Next
    'Debug.Print

    N = 0
    For J = 1 To 2 ' Divide - 8 bit bytes
    For k = 1 To 8
    N = N + 1
    V(J) = V(J) + D(N) * 2 ^ (8 - k)
    Next
    Next

    Return Chr(V(2)) + Chr(V(1)) ' Setup String

    End Function


    Public Function MKS(ByVal X As Single) As String 'As String

    Dim D(24) As Byte, V(3) As Byte
    Dim MxExp As Integer, AExp As Integer
    Dim i As Integer, J As Integer, k As Integer
    Dim N As Integer, E As Single
    Dim Found As Boolean

    If X < 0 Then ' Sign of the number
    D(1) = 1
    Else : D(1) = 0
    End If

    E = CSng(Math.Abs(X)) ' Absolute Value
    Found = False
    For i = 0 To 256 ' Cal Maximum power
    N = 128 - i
    If E >= (2 ^ N) Then
    MxExp = N
    i = 256
    Found = True
    End If
    Next

    If E = 0 Or (Not Found) Then ' Set Max Power for String
    AExp = 0
    Else
    AExp = 129 + MxExp
    E = E - (2 ^ MxExp)
    End If

    For k = 1 To 23 ' Set Binary array
    N = MxExp - k
    If Fix(E / (2 ^ N)) = 1 Then
    D(k + 1) = 1
    E = E - (2 ^ N)
    Else : D(k + 1) = 0
    End If
    Next

    ' For i = 1 To 24
    ' Debug.Print D(i);
    ' Next
    ' Debug.Print

    N = 0
    For J = 1 To 3 ' Divide - 8 bit bytes
    For k = 1 To 8
    N = N + 1
    V(J) = V(J) + D(N) * 2 ^ (8 - k)
    Next
    Next
    ' Set-up String
    Return Chr(V(3)) + Chr(V(2)) + Chr(V(1)) + Chr(AExp)

    End Function


    Public Function MKD(ByVal X As Double) As String 'As String

    Dim D(56) As Byte, V(7) As Byte
    Dim MxExp As Integer, AExp As Integer, C As Integer
    Dim i As Integer, J As Integer, k As Integer
    Dim N As Integer, E As Double
    Dim Found As Boolean
    Dim tMKD As String

    If X < 0 Then ' Sign of the number
    D(1) = 1
    Else : D(1) = 0
    End If

    E = Math.Abs(X) ' Absolute Value
    Found = False
    For i = 0 To 256 ' Cal Maximum power
    N = 128 - i
    If E >= (2 ^ N) Then
    MxExp = N
    i = 256
    Found = True
    End If
    Next

    If E = 0 Or (Not Found) Then ' Set Max Power for String
    AExp = 0
    Else
    AExp = 129 + MxExp
    E = E - (2 ^ MxExp)
    End If

    For k = 1 To 55 ' Set Binary array
    N = MxExp - k
    If Fix(E / (2 ^ N)) = 1 Then
    D(k + 1) = 1
    E = E - (2 ^ N)
    Else : D(k + 1) = 0
    End If
    Next

    ' For i = 1 To 24
    ' Debug.Print D(i);
    ' Next
    ' Debug.Print

    N = 0
    For J = 1 To 7 ' Divide - 8 bit bytes
    For k = 1 To 8
    N = N + 1
    V(J) = V(J) + D(N) * 2 ^ (8 - k)
    Next
    Next

    tMKD = ""
    For i = 1 To 7 ' Set-up String
    k = 8 - i
    tMKD = tMKD + Chr(V(k))
    Next
    tMKD = tMKD + Chr(AExp)

    Return tMKD
    End Function

    End Module

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width