Shortest way to get String from part of Bytes - vb.net

I have read bytes from file and I have to get a String from known location.
Dim b() As Byte = File.ReadAllBytes("MYFILE.BIN")
Dim myYear As String = Encoding.ASCII.GetString(b)
That gives whole file in myYear String so I can extract a year from there.
Is there some handy and shortest way to get bytes from 50 to 54 and convert only that part to string?

Maybe something like the following. Its not shorter but you don't need to read the complete file into memory.
Using stream = File.OpenRead("c:\MYFILE.BIN")
stream.Seek(50, SeekOrigin.Begin)
Dim b = New Byte(4) {}
stream.Read(b, 0, 5)
Dim str = Encoding.ASCII.GetString(b)
End Using

Related

Parsing binary to BigInteger in VB .NET?

How are you?
I wrote a program manipulating big binary chains (string variables). This said manipulation requires me to store my chains in a variable so I can use them as numbers. The only variable type that I have found big enough to store such lengthy numbers is BigInteger (we are talking 1.0E100+).
I would like to use something like:
val = BigInteger.Parse(bin, 2)
But the second parameter needed is a NumberStyles object, which can only refer to a NumberStyles.HexNumber.
Is there a simple/optimal way to do this?
Thank you very much. :)
This converts a binary string to BigInteger in 8 bit chunks. It assumes that the binary string represents a positive number.
Private Function BinToBI(ByRef binstr As String) As BigInteger
Dim t As New List(Of Byte)
Dim s As String
Dim idx As Integer = binstr.Length
Do While idx > 0
'get 8 bits
If idx >= 8 Then
s = binstr.Substring(idx - 8, 8)
Else
s = binstr.Substring(0, idx).PadLeft(8, "0"c)
End If
'convert to byte and add to list
Dim b As Byte = Convert.ToByte(s, 2)
t.Add(b)
idx -= 8
Loop
'force to positive
If t(t.Count - 1) >= 128 Then
t.Add(0)
End If
Dim rv As New BigInteger(t.ToArray)
Return rv
End Function
for testing
Dim d As Double = 1.0E+101
Debug.WriteLine(d.ToString("n2"))
Dim bi As BigInteger
' Dim bin As String = "1111111111111111111111111111111" 'Integer.MaxValue
' Dim bin As String = "111111111111111111111111111111111111111111111111111111111111111" 'Long.MaxValue
Dim bin As String = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110"
bi = BinToBI(bin)
Debug.WriteLine(bi.ToString("n2"))
This was not well tested but should give you some ideas.

How to read HDF5 bytes array resulting of string dataset

I use the HDF5DotNet libraries with VB.net. I need to read a string dataset (3000 items, each item len = 16).
I use a byte array to store all the values but it's not easy to parse : I need to get a string by line and not a part of string. Do you know a better way to store and parse the result ?
Here my code:
'Load the file
Dim HDF5TestFileID As HDF5DotNet.H5FileId
HDF5TestFileID = H5F.open("C:\test.hdf5", H5F.OpenMode.ACC_RDONLY)
'Get datset and group id
Dim GroupRootId As HDF5DotNet.H5GroupId = H5G.open(HDF5TestFileID, "/")
Dim dataSetRN As H5DataSetId = H5D.open(GroupRootId, "MyItemsNames")
'Build byte array from the dataset
Dim readDataBackRN(16 * 3000) As Byte
Dim h5DataBackRN As New H5Array(Of Byte)(readDataBackRN)
Dim typeIdRN As H5DataTypeId = H5D.GetType(dataSetRN)
H5D.read(dataSetRN, typeIdRN, h5DataBackRN)
'try to parse the result but not easy to use data
Dim content as string = System.Text.Encoding.UTF8.GetString(readDataBackRN).Replace(" ", "<br>")
You normally read the buffer, check if the string is complete and if not continue reading and append the new contents to the previous fragment and keep doing that until you find the end of line.
Can you do this?
In this case the string is complete but it's possible that one day I reach the limit of size. I cannot modify the H5D.read method. I wonder if an simple array of byte is the best way ?
The better way would be to store result in a an array of array of bytes (1 array of byte per line of string) and not store all items in a single array of bytes ?
But I don't knwon if it's possible.

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.

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)

Mixed Encoding to String

I have a string in VB.net that may contain something like the following:
This is a 0x000020AC symbol
This is the UTF-32 encoding for the Euro Symbol according to this article http://www.fileformat.info/info/unicode/char/20ac/index.htm
I'd like to convert this into
This is a € symbol
I've tried using UnicodeEncoding() class in VB.net (Framework 2.0, as I'm modifying a legacy application)
When I use this class to encode, and then decode I still get back the original string.
I expected that the UnicodeEncoding would recognise the already encoded part and not encode it against. But it appears to not be the case.
I'm a little lost now as to how I can convert a mixed encoded string into a normal string.
Background: When saving an Excel spreadsheet as CSV, anything outside of the ascii range gets converted to ?. So my idea is that if I can get my client to search/replace a few characters, such as the Euro symbol, into an encoded string such as 0x000020AC. Then I was hoping to convert those encoded parts back into the real symbols before I insert to a SQL database.
I've tried a function such as
Public Function Decode(ByVal s As String) As String
Dim uni As New UnicodeEncoding()
Dim encodedBytes As Byte() = uni.GetBytes(s)
Dim output As String = ""
output = uni.GetString(encodedBytes)
Return output
End Function
Which was based on the examples on the MSDN at http://msdn.microsoft.com/en-us/library/system.text.unicodeencoding.aspx
It could be that I have a complete mis-understanding of how this works in VB.net. In C# I can simply use escaped characters such as "\u20AC". But no such thing exists in VB.net.
Based on advice from Heinzi I implemented a Regex.Replace method using the following code, this appear to work for my examples.
Public Function Decode(ByVal s As String) As String
Dim output As String = ""
Dim sRegex As String = "0x[0-9a-zA-Z]{8}"
Dim r As Regex = New Regex(sRegex)
Dim myEvaluator As MatchEvaluator = New MatchEvaluator(AddressOf HexToString)
output = r.Replace(s, myEvaluator)
Return output
End Function
Public Function HexToString(ByVal hexString As Match) As String
Dim uni As New UnicodeEncoding(True, True)
Dim input As String = hexString.ToString
input = input.Substring(2)
input = input.TrimStart("0"c)
Dim output As String
Dim length As Integer = input.Length
Dim upperBound As Integer = length \ 2
If length Mod 2 = 0 Then
upperBound -= 1
Else
input = "0" & input
End If
Dim bytes(upperBound) As Byte
For i As Integer = 0 To upperBound
bytes(i) = Convert.ToByte(input.Substring(i * 2, 2), 16)
Next
output = uni.GetString(bytes)
Return output
End Function
Have you tried:
Public Function Decode(Byval Coded as string) as string
Return StrConv(Coded, vbUnicode)
End Function
Also, your function is invalid. It takes s as an argument, does a load of stuff and then outputs the s that was put into it instead of the stuff that was processed within it.