P
Paulo Becker
Hi!
I'm coding an RC4 simulator, and in order to manipulate some data I've
created functions that transform strings into byte arrays (where each
array position contains the ASCII code of the character in the
string), and from byte arrays into strings.
Here they are:
Public Shared Function StringToByteArray(ByVal strString As
String) As Array
Dim arrByteArray(strString.Length - 1) As Byte
For intI As Integer = 0 To strString.Length - 1
arrByteArray(intI) =
CByte(Microsoft.VisualBasic.Asc(strString(intI)))
Next
Return arrByteArray
End Function
Public Shared Function ByteArrayToString(ByVal arrByteArray As
Array) As String
Dim strString As String = ""
For intI As Integer = 0 To UBound(arrByteArray)
strString &= Chr(arrByteArray(intI))
Next
Return strString
End Function
The "StringToByteArray" function works great, and the performance
seems acceptable. However, when I run "ByteArrayToString", the
performance is orders of magnitude inferior to that of its
counterpart. With an array of about 400,000 positions, it took around
10 minutes to create the string.
I'm not sure if the bottleneck is in the "&=" operator or in Chr().
Does anyone have any idea, or any suggestions as to what I could do to
make this more efficient?
Thank you very much for your attention.
I'm coding an RC4 simulator, and in order to manipulate some data I've
created functions that transform strings into byte arrays (where each
array position contains the ASCII code of the character in the
string), and from byte arrays into strings.
Here they are:
Public Shared Function StringToByteArray(ByVal strString As
String) As Array
Dim arrByteArray(strString.Length - 1) As Byte
For intI As Integer = 0 To strString.Length - 1
arrByteArray(intI) =
CByte(Microsoft.VisualBasic.Asc(strString(intI)))
Next
Return arrByteArray
End Function
Public Shared Function ByteArrayToString(ByVal arrByteArray As
Array) As String
Dim strString As String = ""
For intI As Integer = 0 To UBound(arrByteArray)
strString &= Chr(arrByteArray(intI))
Next
Return strString
End Function
The "StringToByteArray" function works great, and the performance
seems acceptable. However, when I run "ByteArrayToString", the
performance is orders of magnitude inferior to that of its
counterpart. With an array of about 400,000 positions, it took around
10 minutes to create the string.
I'm not sure if the bottleneck is in the "&=" operator or in Chr().
Does anyone have any idea, or any suggestions as to what I could do to
make this more efficient?
Thank you very much for your attention.