How do read from a NotesStream character by character. i.e one character at a time in a loop. NoteasStream.Read(1) reads one character but returns a variant array which I am not able to convert to the specific character.
This way you can read byte by byte from stream
Dim stream As NotesStream
Dim bytes As variant
...
Do
bytes = stream.Read(1)
Print bytes(0)
Loop Until stream.IsEOS
Probably more efficient is it to read more then just one byte at a time from stream
Dim stream As NotesStream
Dim bytes As variant
...
Do
bytes = stream.Read(32767)
ForAll b In bytes
Print b
End ForAll
Loop Until stream.IsEOS
If you want to get characters instead of bytes one by one then you can use this
Dim stream As NotesStream
...
Dim buffer As String
Dim i As Long
Dim char As String
buffer = stream.ReadText()
For i=1 To Len(buffer)
char = Mid(buffer, i, 1)
Print char
Next
Related
I want to split my long array to 2bytes (4 digit HEX) or 4bytes(8 digit HEX). If c value is 1, I want to get 2bytes (4 digit HEX) HEX array and if c value is 0, I want to get 4bytes(8 digit HEX) HEX array from a long HEX string.
After that, I want to convert while converting 2byte or 4byte to floating number( decimal number).
I have code for 4byte to a decimal value.
Dim bytes() As Byte = {&H43, &H62, &HD3, &HE}
If BitConverter.IsLittleEndian Then
Array.Reverse(bytes)
End If
Dim myFloat As Decimal = BitConverter.ToSingle(bytes, 0)
txt4.Text = myFloat
Please provide me the code for this function.
Example:
Long Hex value - 4362D30EFFC00000FFC00000FFFFFFFF
If C is 1, split 4 digit HEX values.
4362
Then convert to decimal.
17250
Again split 4 digit HEX values.
D30E
Then convert to decimal
-11506
If c is 0, split 8 digit HEX values.
4362D30E
Then convert to decimal
226.824432
Please help with this. I don't not know much about VB.Net
I'm going to give this a shot as well, but do consider what I said on your newer question.
To read a certain amount of bytes and turn them into a number best is to use the BitConverter and its To*** methods.
This is how many bytes each numeric data type uses:
Double: 8 bytes
Int16 / UInt16 / Short / UShort: 2 bytes
Int32 / UInt32 / Integer / UInteger: 4 bytes
Int64 / UInt64 / Long / ULong: 8 bytes
Single: 4 bytes
With this in mind, all you need is to decide on a data type based on the number of bytes you want to read, and then specify an index in your array where to start reading from.
Remember that if you reverse your array you have to start reading from the end of the array instead.
Dim StartIndex As Integer = 0
Dim EndIndex As Integer = bytes.Length - 1
'How many bytes to read: 2 or 4.
Dim BytesPerStep As Integer = If(C = 1, 2, 4)
'If the system uses little endianness we need to reverse the array and loop backwards.
If BitConverter.IsLittleEndian Then
'Reverse the array.
Array.Reverse(bytes)
'Swap start and end index.
StartIndex = EndIndex
EndIndex = 0
'Negate BytesPerStep: Go backwards.
BytesPerStep = -BytesPerStep
End If
'Iterate the array <BytesPerStep> bytes at a time.
For i = StartIndex To EndIndex Step BytesPerStep
If C = 1 Then
'Read two bytes = Short (Int16).
ListBox1.Items.Add(BitConverter.ToInt16(bytes, i))
Else
'Read four bytes = Single (Float)
ListBox1.Items.Add(BitConverter.ToSingle(bytes, i))
End If
Next
I need to compare phone numbers from a CSV file to phone numbers in an SSMS database in VB6 without using the .Net Library. One may have a number as 555-555-5555 and the other may have the same number as (555) 555-5555 which obviously kicks back as different when strings are compared.
I know I can use for loops and a buffer to pull out only numeric characters like:
Public Function PhoneNumberNumeric(PhoneNumberCSV As String) As String
Dim CharNdx As Integer
Dim buffer As String
For CharNdx = 1 To Len(PhoneNumberCSV) Step 1
If IsNumeric(Mid(PhoneNumberCSV, CharNdx, 1)) Then
buffer = buffer + Mid(PhoneNumberCSV, CharNdx, 1)
End If
Next
PhoneNumberNumeric = buffer
End Function
but this is expensive. Is there a less expensive way to do this?
This should be a bit quicker:
Private Function Clean(ByRef Original As String) As String
Dim I As Long
Dim J As Long
Dim Char As Long
Clean = Space$(10)
For I = 1 To Len(Original)
Char = AscW(Mid$(Original, I, 1))
If 48 <= Char And Char <= 57 Then
J = J + 1
If J > 10 Then Exit For 'Or raise an exception.
Mid$(Clean, J, 1) = ChrW$(Char)
End If
Next
End Function
It avoids string concatenation, ANSI conversions, and VBScript-form "pigeon VB" (use of slow Variant functions).
I am trying to read all of the integers in a list box line.
Dim scores As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "\d+")
I have saved the scores in a format like this
Name 00 00 00
Could I have the regular expression for reading 3 integers with a space in between each number? The file the integers are stored in is a notepad file.
Approach without regex:
Const SEPARATOR As String = " "c
Dim line As String 'Here reading line from the file
Dim numbers As New List(Of Int32)()
Dim values As String() = line.Split(SEPARATOR) 'Split values to array
For Each value As String in values
Dim tempnumber As Int32
If Int32.TryParse(value, tempnumber) = True Then
'Accept only numbers
numbers.Add(tempnumber)
End If
Next
Use Matches instead of Match and store the results of the Regex into a MatchCollection
Sub Main()
Dim scores As String = "00 13 00"
Dim score As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(scores, "\d+")
For i As Integer = 0 To score.Count - 1
Console.WriteLine(score.Item(i))
Next
Console.ReadLine()
End Sub
Results:
I have a very large binary file 250 gigs working to split this file into smaller files starting from a certain bytes header until a new header or end-of-file is reached i want this part to be stored as a separate new file for each case like this.
looking for certain byte pattern.(header)
have to save all the data till first occurrence (excluding the header).
starting from the first occurrence of header all data will be save until a second header is found in a separate file.
all the data from first occurrence till end of file will be stored as separate file.
Sub hexDumpFile(fil As String)
Using reader As New BinaryReader(File.Open(fil, FileMode.Open))
' Loop through length of file.
Dim pos As Integer = 0
Dim length As Long = reader.BaseStream.Length
'looking up for 00 00 01 BA 44 00 04 00 04
Dim header(9) As Byte
Dim buff(18) As Byte
header = {&H0, &H0, &H1, &HBA, &H44, &H0, &H4, &H0, &H4}
buff.Initialize()
Dim i As Integer
While pos < length - buff.Length
'--- Read the integer.
buff.Initialize()
buff = reader.ReadBytes(buff.length)
Console.Write(Hex(pos) & ":")
' Write to screen.
Try
For i = 0 To buff.Length
Console.Write(Hex(buff(i))) hex value
Console.Write(" ") ' space
Next
Console.WriteLine("") ' line break
Catch EX As Exception
Console.WriteLine(EX.Source.ToString)
Console.WriteLine(EX.Message)
End Try
' Add length of integer in bytes to position.
pos += 1
End While
End Using
End Sub
Problems:
can not avoid exceptions --> Index was outside the bounds of the array.
can not find first match
can not save separate files
I have a variable that holds a string "02100030000000000000000000D5010008D501000804" and I'm byte separating the string using
For i As Integer = 1 To (stringReader.Length - 1) Step 2
'Get the successive 2-character substrings, parse as bytes and add to total
Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
sum = sum + CInt(b)
Next
I'm converting the strings to direct integers.e.g:(string"10" to Integer10 and ). That works fine. But Whenever I convert the string"02" to Integer, I get only Integer(2) and I need Integer(02). How can I proceed with?
My code is:
stringReader = fileReader.ReadLine()
byt = stringReader(1) + stringReader(2)
stringreader contains something like "100030000000000000000000D5010008D501000804"
Byte separation
For i As Integer = 1 To (stringReader.Length - 1) Step 2
'Get the successive 2-character substrings, parse as bytes and add to total
Dim b As Byte = Byte.Parse(stringReader.Substring(i, 2), NumberStyles.AllowHexSpecifier)
sum = sum + CInt(b)
Next
You can use
number.ToString("D2")
where number is an integral type like System.Int32(Integer).
Further reading: Standard Numeric Format Strings: The Decimal ("D") Format Specifier
If you have a String instead you could also use String.PadLeft:
"2".PadLeft(2, "0"c) ' -> "02"