slcotten
September 27th, 2001, 02:26 PM
Hello....
I have a array of a user defined type that I am adding to a file like this:
Open FileName for binary as #nFileNum
for nCount = LBound(g_MyArray) to UBound(g_MyArray) step 1
Put nFileNum, , g_MyArray(nCount)
next nCount
' close the file
Close nFileNum
Now I want to add some integers to the file that is not in the array. What is the best way to do that?
Much thanks in advance for your help.
Iouri
September 27th, 2001, 02:45 PM
I think you can convert integer to binary and them to the file. Here is the code how to convert decimal to binary
Private Sub Form_Load()
txtDecimalValue.Text = "123456789"
End Sub
Private Sub txtDecimalValue_KeyPress(KeyAscii As Integer)
'Cancel input if the key pressed is not a number
If IsNumeric(Chr(KeyAscii)) = False Then
KeyAscii = 0
End If
End Sub
Function Bin(Number) ' both Variants
'===========================================
Dim BitStream() As Byte
Dim Bits As Byte
Dim BitPointer As Byte
Dim HexString As String
Dim lenHS As Integer
Dim HalfByte As Byte ' smallest VB word
Dim i As Integer
Dim j As Integer
Const DABug As Boolean = False
On Error GoTo eror
Select Case VarType(Number)
Case vbNull
'pass a null, return a null
Bin = Number
Exit Function
Case vbByte
' 0 to 255 unsigned
Bits = 8
Case vbInteger, vbBoolean
' -32,768 to 32,767
Bits = 16
Case vbLong
' -2,147,483,648 to 2,147,483,647
Bits = 32
Case vbSingle, vbDouble
Bits = 32
' HEX() will convert these
' to Longs (if possible!)
Case vbString
'-a Number of unspecified Type-
''Note: This expects a String to be a Number
'' To get the bits for a String, try this code:
''Dim BS() As Byte
''BS = MyString '-or- BS = StrConv(MyString, vbFromUnicode)
''For i = LBound(BS) To UBound(BS)
'' Print Bin(BS(i)),
''Next i
Case Else
Err.Raise 2001, , "Not sure HOW to handle a (" & TypeName(Number) & ") DATA TYPE?"
End Select
'' if HEX() can't handle it,
'' then neither can this code!
HexString = Hex(Number)
lenHS = Len(HexString)
If Bits = 0 Then Bits = lenHS * 4 'bits
ReDim BitStream(1 To Bits)
BitPointer = Bits
For i = lenHS To 1 Step -1
HalfByte = Val("&H" & Mid(HexString, i, 1))
For j = 1 To 4 ' bits
If HalfByte Then
BitStream(BitPointer) = HalfByte Mod 2
HalfByte = Fix(HalfByte / 2)
End If
BitPointer = BitPointer - 1
Next j
Next i
'now add 48 to all bits(bytes) for "0" or "1"
For i = 1 To Bits
BitStream(i) = BitStream(i) + 48
Next i
Bin = StrConv(BitStream, vbUnicode)
' format it
Bin = Trim(Format(Bin, "&&&&&&&& &&&&&&&& &&&&&&&& &&&&&&&&"))
'(format it here? or outside this proc?)
Exit Function
eror:
If DABug Then Debug.Print Err.Number & "-" & Err.Description
Err.Clear
Bin = "0"
End Function
Private Sub txtDecimalValue_Change()
lblBinaryValue.Caption = Bin(txtDecimalValue.Text)
End Sub
Iouri Boutchkine
iouri@hotsheet.com
slcotten
September 27th, 2001, 03:03 PM
Thanks for your reply.... However I must have been unclear in my question. When adding the Array items (hereafter called records) using Statement Put to the file it adds them according the last record position. You can then Get the records in a similar fashion:
[vbcode]
nCount = 1
Open FileName For Binary As #nFileNum
Do
ReDim Preserve g_MyArray(nCount)
Get nFileNum, , g_MyArray(nCount)
' tells us if we are on the last record in file
If Loc(nFileNum) > LOF(nFileNum) Then
' If so Extract last record
ReDim Preserve g_OpenTree(LBound(g_OpenTree) To (nCount - 1))
' Finished opening file
Exit Do
End If
nCount = nCount + 1
Loop
[/vbcode}
What I would like to do is Put and Get variables BEFORE we Put and Get the Array records. How is this done? Hope this clarifies my query a little.... if not reply and say so.
Much thanks again for your help.
slcotten
September 28th, 2001, 08:00 AM
Oooppss... the correct code for the previous post is this:
nCount = 1
Open FileName for binary as #nFileNum
Do
ReDim Preserve g_MyArray(nCount)
get nFileNum, , g_MyArray(nCount)
' tells us if we are on the last record in file
If Loc(nFileNum) > LOF(nFileNum) then
' If so Extract last record
ReDim Preserve g_MyArray(LBound(g_MyArray) to (nCount - 1))
' Finished opening file
Exit Do
End If
nCount = nCount + 1
Loop