Output/write SIMH DEC tape file to disk - vb.net

--->>> In VB .Net
I was interested in writing a simulated disk file in SIMH file format. Essentially ASCII records are encapsulated with Tape (FILE) Control characters and binary record lengths. I know this is easily handled in C/C++ but I would like to implement this in VB.Net
First, Is there anyone can put an example on in writing table control characters to a data stream for output to a flat file.
Second, Example of writing to the SIMH format. I have have a PDF describing the the SIMH format so I'm reviewing this specification.
Thirdly, using the BitConverter to export a stream with binary tape markers encapsulating ASCII records. I believe i could use the BitConverter to create the binary file markers. However it seems I can use BinaryWriter, setting up a FileStream Handle. If write the binary tape control characters with BinaryWriter how to implement one writer writing both Binary record lengths and the ASCII record? An example would be ideal.
SIMH is a derivative DEC tape format.

Dim fs As FileSystem = Nothing
Dim infile As String = Path.Combine(<path_to_file>)
fs = FileSystem(infile, FileMode.Create)
Using bw as BinaryWriter = New BinaryWriter(fs)
For i as integer = 0 to Headers.Count - 1
Dim lengthHeader As Integer = m_headers(i).Length
If lengthHeader = MAX_HEADER_LENGTH then
Dim dataArray() As byte = StringtoByteArray(m_headers(i))
bw.Write(lengthHeader)
bw.Write(dataArray)
bw.Write(lengthHeader)
End If
Next
For i As Integer = 0 To m_myRecs.Count - 1
Dim thisRec As String = m_myRecs(i)
Dim lengthRec As Integer = thisRec.Length
Dim dataSentinal = (row * 6) + 8 + 2
bw.Write(dataSentinal)
Dim dataArray() As byte = StringtoByteArray(m_myRecs(i))
bw.Write(dataArray)
bw.Write(dataSentinal)
Next
bw.Write(-1)
End Using
fs.Close()

Related

VB - BinaryWriter inserting junk characters

I'm trying to create an MP3 ID3v1 Tag editor in Visual Basic (2010)
I have no problem reading tags, however, I can't seem to update the tags correctly.
I use FileStream to open the file, and then I use BinaryWriter. I seek to right after the "TAG" header, which is 125 bytes from the end of file.
The first field is the Title field, which is 30 characters long.
So before writing the new tag, I would clear it by writing 30 spaces.
Then I seek back to the beginning, and try to write the new data. But it ends up overwriting data in the next field.
Dim file As New System.IO.FileStream(songpath, IO.FileMode.Open)
Dim bw As New System.IO.StreamWriter(file)
file.Seek(-125, IO.SeekOrigin.End)
bw.Write(" ")
file.Seek(-125, IO.SeekOrigin.End)
bw.Write(data(0))
bw.Close()
file.Close()
Here is a screen cap of the result. I attempted to write "Test" in the title field, I write the data and then reopen the file and this is what I get.
Rather than using file.Seek, I would set the .Position property, like this:
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim f = "C:\temp\sample.MP3"
Dim newTitle = "This is a test."
Dim dataLen = 30
Dim titleData(dataLen - 1) As Byte
Dim newTitleBytes = Encoding.ASCII.GetBytes(newTitle)
Array.Copy(newTitleBytes, titleData, Math.Min(dataLen, newTitleBytes.Length))
Using str As New FileStream(f, FileMode.Open, FileAccess.Write, FileShare.None)
Dim titlePosition = str.Length - 125
str.Position = titlePosition
str.Write(titleData, 0, dataLen)
End Using
End Sub
End Module
The unused portion of the title should be bytes with a value of zero - when you create an array its values are set to zero* for you. Then you can fill the start of the title bytes with the bytes which represent the string in ASCII. I suspect you could get away with using ISO-8859-1 if you need accented characters, but don't rely on that.
The Using construction makes sure that the file is closed afterwards even if something goes wrong.
* If it's an array of a numeric type, like Byte or Integer and so on.

VB Hex to DocX Object

I have a selection of docx files stored as blob data in hexadecimal, I need to retrieve these so I can access the text within.
So far, I have converted the hex to string format with the following:
Dim blob = BLOB DATA
Dim con As String = String.Empty
For x = 2 To st.Length - 2 Step 2
con &= ChrW(CInt("&H" & st.Substring(x, 2)))
Next
However, if I then save the output from this as a .docx the file will not open because it is 'corrupt'. I presume that is why when I load this string into a memorystream and then try and use Novacode.DocX.Load(memoryStream) it gives me a similar corruption error.
I have tried splitting to byte array in two fashions, both give me different results.
System.Text.Encoding.Default.GetBytes(hex)
I have also tried.
Public Function HexToByteArray(hex As String) As Byte()
Dim upperBound As Integer = hex.Length \ 2
If hex.Length Mod 2 = 0 Then
upperBound -= 1
Else
hex = "0" & hex
End If
Dim bytes(upperBound) As Byte
For i As Integer = 2 To upperBound
bytes(i) = Convert.ToByte(hex.Substring(i * 2, 2), 16)
Next
Return bytes
End Function
I then tried converting them both to a memory stream and using them to create a DocX object like so:
Dim doc As DocX = DocX.Load(New MemoryStream(bytes))
docx is not a text format, it's a binary format. Thus, converting it to a string is just plain wrong. Your end result needs to be a byte array.
Knowing that, your problem can be split into two simpler problems:
Split your hex string into strings of two characters each. See this SO question for details (or keep your existing loop, which is perfectly fine):
How to split a string by x amount of characters
Convert those "small" strings, which contain the hexadecimal representation of a byte, into bytes. See this SO question for details:
How do I convert a Hexidecimal string to a Byte Array?
Combining those two solutions is left as an exercise to the reader. We don't want to spoil all the fun or ruin the learning experience. ;-)

How to replace bytes in VB.NET?

I have two strings:
Dim Original_Hex_Bytes as string = "616572646E61"
Dim Patched_Hex_Bytes as string = "616E64726561"
Then I have a binary file and I need to search for the Original_Hex_Bytes and replace them with Patched_Hex_Bytes; I don't konw the offset where begin to write new bytes :(
How can I do this?
If needed, I also know how to convert Hex strings in bytes, I use this:
Private Function Hex_To_Bytes(ByVal strinput As String) As Byte()
Dim i As Integer = 0
Dim x As Integer = 0
Dim bytes(strinput.Length / 2) As Byte
Do While (strinput.Length > i + 1)
Dim lngDecimal As Long = Convert.ToInt32(strinput.Substring(i, 2), 16)
bytes(x) = Convert.ToByte(lngDecimal)
i += 2
x += 1
Loop
Return bytes
End Function
You can use BinaryReader and BinaryWriter classes to achieve this.
But in this case, as you do not know the file structure, need to read the entire file and sweep it in search of bytes array and will be easier to use ASCII strings as aerdna and andrea.
When you know the structure of a file is more appropriate to work with data structure to manipulate its contents.

Can we read dbisam .dat files through vb.net

I am using a 3rd party account software which uses a dbisam database and have tables with .dat extension.
is there any way to read dbisam tables in vb.net?
I am getting quite difficult to read dbisam in visual basic, is there any guide of tip available, please provide your input.
Here is the code I tried but nothing is working
Dim contents As New StringBuilder()
Dim enc As Encoding = New ASCIIEncoding()
'flDatFile is the source of .dat file
Using fStream As FileStream = New FileStream(flDatFile, FileMode.Open, FileAccess.Read)
' The maximum bytes to read.
Dim toRead As Int32 = 1024000 ' 1Kb
' The buffer for read bytes.
Dim buffer(toRead - 1) As Byte
' The number of bytes that have been read.
Dim read As Int32 = fStream.Read(buffer, 0, toRead)
' While there is any data that has been read
Do While read > 0
' Get the string from the byte array.
MsgBox(enc.GetString(buffer, 0, read))
' Read next batch of data into the buffer.
read = fStream.Read(buffer, 0, toRead)
Loop
fStream.Close()
End Using

Reading a part of a txt file in VB.NET

I need to read a txt file part by part...
For example, in a txt file: (age,5char_name)
17susan23wilma25fredy
I need to read firstly 17susan. In other words, the first seven characters and after 23wilma and 25fredy, but I'm not reading the whole file and substring the file record. Is there a way to do this via streamreader?
Records are always seven bytes... 2 bytes for age, 5 bytes for name and all records in a line. There is no jump into the next line.
I think there is the solution:
Dim filestream As New FileStream("\records.txt", FileMode.Open)
Dim streamreader As New StreamReader(fs)
Dim buffer(7) As Char
bw.ReadBlock(buffer, 0, 7)
Console.WriteLine(buffer)
this is read first 7.. you can read other via loop or for..
If you ALWAYS want to use the first 7 characters, you can simply use Visual Basic line of code like:
Dim Result as String = Microsoft.VisualBasic.Left(string, no)
String is the StreamReader line from which you want to read only first seven characters.
No is any integer value and it is equal to the number of characters you want to read, in this case 7.
Also, in the same way, you can use Right and Mid to select a string from the right or somewhere in the middle.
Assuming a static number of characters per record (seven in your description), you could use a FileStream reader instead, and use the Read method to retrieve seven chars at a time.
For example:
Const chunkSize As Integer = 7
Using inputFile = File.OpenRead("namefile.txt")
Dim bytesRead As Integer
Dim buffer = New Byte(chunkSize - 1) {}
bytesRead = inputFile.Read(buffer, 0, buffer.Length)
while bytesRead = 7
'Process the buffer here
bytesRead = inputFile.Read(buffer, 0, buffer.Length)
End While
End Using
(code isn't tested, but should be close)