Converting string array to byte array and back - arrays

I'm trying to convert a string array to byte array. I would like to use Encoding.Default.GetBytes() but it only accepts String and Char(). This is the code
Dim aStringArray(2) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim stringArrayBytes() As Byte = Encoding.Default.GetBytes(aStringArray) `this is wrong

If you are trying to combine the array into a single byte array try this
Dim aStringArray(1) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim stringArrayBytes As New List(Of Byte)
For Each s As String In aStringArray
stringArrayBytes.AddRange(System.Text.Encoding.Default.GetBytes(s))
Next
Dim bytarray() As Byte = stringArrayBytes.ToArray

Here little bid different approach, based on your example :)
Dim aStringArray(2) As String
aStringArray(0) = "FileName"
aStringArray(1) = "FileSize"
Dim allStrngItems As String = String.Join(String.Empty, aStringArray)
Dim allBytes As Byte() = Encoding.Default.GetBytes(allStrngItems)

Related

Adding Regex matches to an array

I'm struggling to solve a small bit of code. What the code does is to first load a CSV file, line by line (starting by line 3), and add it to an array. Then run a regex match and I want to insert the value in an array.
This is my working code, it shows a msgbox with the actual matches:
Dim file = "C:\Users\David\Desktop\mycsv.csv"
Dim basestatisticspath = Path.GetDirectoryName(file)
Dim statistics() As String = IO.File.ReadAllLines(file)
'Dim x As Integer = statistics.Length
'ReDim Preserve statistics(x)
Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
Dim linefilename As Match = Nothing
Dim langmatch As Match = Nothing
Dim filename() As String
Dim lang() As String
For i = 2 To UBound(statistics)
langmatch = regexlang.Match(statistics(i))
linefilename = regexlinefilename.Match(statistics(i))
MsgBox(langmatch.Value & linefilename.Value)
Next
That works well and the actual matches is what I want. So my next step was to add each match to an array to then use it to generate other files.
Therefore I ended up with this:
Dim file = "C:\Users\David\Desktop\myscv.csv"
Dim basestatisticspath = Path.GetDirectoryName(file)
Dim statistics() As String = IO.File.ReadAllLines(file)
'Dim x As Integer = statistics.Length
'ReDim Preserve statistics(x)
Dim regexlang As Regex = New Regex("(?<=^"")\[.*\]")
Dim regexlinefilename As Regex = New Regex("(?<=^""\[.*?\]\s).*(?="")")
Dim linefilename As Match = Nothing
Dim langmatch As Match = Nothing
Dim filename() As String
Dim lang() As String
' Set all value line by line
For i = 2 To UBound(statistics)
langmatch = regexlang.Match(statistics(i))
linefilename = regexlinefilename.Match(statistics(i))
lang(i) = langmatch.Value.ToString
filename(i) = linefilename.Value.ToString
MsgBox(langmatch.Value & linefilename.Value)
Next
After adding the below the program crashes on that line
lang(i) = langmatch.Value.ToString
filename(i) = linefilename.Value.ToString
I am assuming you can add the value of a regex match to a certain position of a string, but it seems I am wrong.
I´ve been searching for an answer with no results (at least to my poor understanding).
How could I convert each of the matches to a string and add it to the i position of the array?
Thanks in advance!
UPDATE:
As #Tval explained, I solved it by including the size of the array when declaring it. Thanks!
Dim filename(UBound(statistics)) As String
Dim lang(UBound(statistics)) As String
you need to initialize the array before you can reference it or you'll get a null reference error, also you can't reference an index that doesn't exist yet or you'll get an index out of range exception.
right now your using an array with a fixed length, so if you want to add a value to it you'll have to re-declare it one index larger every time.
If you want an array of a variable length id suggest using a list, so you can just append values to it without any issues
Dim myList = New List(Of String)
For Each foo As String In bar
myList.Add(bar)
Next

Cannot convert array of bytes to string

I have the code below:
Dim base64Decoded As String
Dim base64Encoded As String = "aGVsbG8="
Dim data As Byte()
data = System.Text.ASCIIEncoding.ASCII.GetBytes(base64Encoded)
base64Decoded = System.Convert.FromBase64String(data)
MsgBox(base64Decoded)
However, I'm getting an error message at the base64Decoded = ... line:
Value of type 'Byte()' cannot be converted to 'String'.
Any ideas?
First of all, you have to convert this string to an array of bytes using System.Convert.FromBase64String then convert it to String using System.Text.ASCIIEncoding.ASCII.GetString , FromBase64String take a string as parameter. Just try the following:
base64Decoded = System.Text.ASCIIEncoding.ASCII.GetString(System.Convert.FromBase64String(base64Encoded))
in your case aGVsbG8= will be converted to:
hello
Reference
ASCIIEncoding.GetString Method
Convert.FromBase64String Method

element of array to string in Visual Basic

I have this code written in Visual basic:
Dim directory As String = Application.StartupPath()
Dim dirinfo As New DirectoryInfo(directory)
Dim filesnatt As String() = dirinfo.GetFiles("*.nat")
Dim filenatt As String
For Each filenatt In filesnatt
Dim filenat As String = Str(filenatt)
Using re As StreamReader = New StreamReader(filenat)
Dim val As String = re.ReadLine()
If val.Contains(TextBox2.Text) Then
Dim a1 As String = filenat
a1 = a1.Remove(".nat")
ComboBox2.Items.Add(a1)
End If
End Using
Next
But I get this error: Value of '1 - dimensional array of System.IO.FileInfo' cannot be converted to '1 - dimensional array of String' because 'System.IO.FileInfo' is not derived from 'String'.
How to fix that?
thats because GetFiles returns FileInfo instead of String change
Dim filesnatt as String() to Dim filesnatt as FileInfo()
and
Dim filenatt as String to Dim filenatt as FileInfo
and in the For Each loop use
Dim filenat as String = filenatt.FullName

Visual Basic: How to get entire section before/after a certain character?

I have a string. For example stringone/string2
How do I get the section before the "/" and also after the "/"
Dim Word As String = "stringone/string2"
Dim wordArr As String() = Word.Split("/")
Dim stringBefore As String = wordArr(???)
Dim stringBefore As String = wordArr(???)
What is the next step
Split() returns an Array. The first element is at Index 0 (zero), the second element is at Index 1 (one), etc...
You should check to make sure the returned array is your expected size (at least), otherwise you'll get an error attempting to access an index slot that doesn't exist.
Dim Word As String = "stringone/string2"
Dim wordArr As String() = Word.Split("/")
If wordArr.Length = 2 Then
Dim stringBefore As String = wordArr(0)
Dim stringAfter As String = wordArr(1)
Debug.Print("stringBefore = " & stringBefore)
Debug.Print("stringAfter = " & stringAfter)
End If
*By the way, the code you posted is not VB6, it's VB.Net.

Converting char array into string array in VB.NET

I am trying to convert an array of characters into a string array (where each character becomes a string), as I need it to be a string array for some processing on the array later in the program. Here is the code I am using:
Dim inputexpression As String = UCase(txtInput.Text)
Dim arrinputexpressionchar() As Char = inputexpression.ToCharArray()
Dim arrinputexpression() As String
For i = 0 To arrinputexpressionchar.Length
arrinputexpression(i) = Char.ToString(arrinputexpressionchar(i))
Next
However, this throws up a 'NullReferenceException was unhandled' (Object reference was not set to an instance of an object) error. Why does this code not work?
You have declared but not initialized the string array.
You could use LINQ:
Dim charsAsStringArray = inputexpression.
Select(Function(c) c.ToString()).
ToArray()
Here's the non-linq way:
Dim strArray(inputexpression.Length - 1) As String
For i = 0 To charArray.Length - 1
strArray(i) = inputexpression(i).ToString()
Next

Resources