PDA

Click to See Complete Forum and Search --> : MKI Function?


HaxSoft
May 26th, 2000, 01:49 AM
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

HaxSoft
Jun 8th, 2000, 10:53 PM
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.

dilum
May 1st, 2011, 04:37 AM
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]

:wave: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
:wave: