how to do vb.net replace on a byte array - vb.net

I have a byte array of data. How can I find and replacement a part of the byte array with replacement data?
dim foo as byte
foo = MY DATA
If foo was a string, then I would do:
foo = replace( foo, target, replacement )
But foo is a byte array. How can I replace?

Just for the fun of it, here is an implementation:
<TestMethod()>
Public Sub Test_ArrayReplace()
AssertEqualArray({1, 4}, ArrayReplace({1, 2, 3}, {2, 3}, {4}))
AssertEqualArray({1, 4, 5}, ArrayReplace({1, 2, 3, 5}, {2, 3}, {4}))
AssertEqualArray({1, 4, 2}, ArrayReplace({1, 2, 3, 2}, {2, 3}, {4}))
AssertEqualArray({1, 4, 5, 2}, ArrayReplace({1, 2, 3, 2}, {2, 3}, {4, 5}))
AssertEqualArray({1, 2, 3, 8, 9}, ArrayReplace({1, 2, 3, 8, 2, 3, 4}, {2, 3, 4}, {9}))
AssertEqualArray({1, 68, 69, 70, 255}, ArrayReplace({1, 65, 66, 67, 255}, "ABC", "DEF", Encoding.ASCII))
End Sub
Private Sub AssertEqualArray(expected() As Byte, actual() As Byte)
Assert.IsNotNull(expected, "expected")
Assert.IsNotNull(actual, "actual")
Assert.AreEqual(expected.Length, actual.Length, "length")
For index = 0 To actual.Length - 1
Assert.AreEqual(expected(index), actual(index), String.Format("index: {0}", index))
Next
End Sub
Public Function ArrayReplace(data() As Byte, find As String, replacement As String, enc As Encoding) As Byte()
Return ArrayReplace(data, enc.GetBytes(find), enc.GetBytes(replacement))
End Function
Public Function ArrayReplace(data() As Byte, find() As Byte, replacement() As Byte) As Byte()
Dim matchStart As Integer = -1
Dim matchLength As Integer = 0
Using mem = New IO.MemoryStream
For index = 0 To data.Length - 1
If data(index) = find(matchLength) Then
If matchLength = 0 Then matchStart = index
matchLength += 1
If matchLength = find.Length Then
mem.Write(replacement, 0, replacement.Length)
matchLength = 0
End If
Else
If matchLength > 0 Then
mem.Write(data, matchStart, matchLength)
matchLength = 0
End If
mem.WriteByte(data(index))
End If
Next
If matchLength > 0 Then
mem.Write(data, data.Length - matchLength, matchLength)
End If
Dim retVal(mem.Length - 1) As Byte
mem.Position = 0
mem.Read(retVal, 0, retVal.Length)
Return retVal
End Using
End Function
The implementation is surely not perfect and might even be buggy.
Keep in mind that in order to convert a String into an ByteArray you have to know the Encoding of the String.
You can call it this way:
foo = ArrayReplace(foo, "find", "replace", Encoding.UTF8)

Related

Assign an integer variable to an integer variable and change name dynamic

Dim LastNumber as Integer = 1
Dim num_0() as Integer = {1, 2, 3, 4, 5}
Dim num_1() as Integer = {6, 7, 8, 9, 10}
Dim num_2() as Integer = {20, 21, 14, 36, 0}
Dim y() As Integer
y(0) = num_0(2)
When I use this code it executes perfectly
But the problem is I want to change the "0" in num_0(2)
When I do...
y(0) = num_ & LastNumber & (2)
This doesnt work
Or
y(0) = ("num_" & LastNumber & "(2)")
This gives me an error that converting a string to an integer is not possible
My question is How can I replace the "0" in num_0(2) with the LastNumber integer variable... so it reads the "8" out of the array num_1(2)
You can use multidimensional array (AKA rectangular array):
Dim num As Integer(,) = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {20, 21, 14, 36, 0} }
y(0) = num(LastNumber, 2)
or jagged array (array of arrays) :
Dim num As Integer()() = { ({1, 2, 3, 4, 5}), ({6, 7, 8, 9, 10}), ({20, 21, 14, 36, 0}) }
y(0) = num(LastNumber)(2)

UID Validation - Verhoeff's Algorithm

I am using MS Access & SQL.
I wish to validate 0.1 Mn UID Numbers (Aadhar Cards) for their authenticity.
The UID is based on Verhoeff's Algorithm.
I did find some queries online. They pertained to C++/Java.
Is there a query which validates 0.1 Mn in MS Access for SQL?
As mentioned by Gord Thempson in the comments, you can find the VBA (and many more implementation on the Wikipedia page
For the sake of completeness,
''' <summary>
''' For more information cf. http://en.wikipedia.org/wiki/Verhoeff_algorithm
''' Dihedral Group: http://mathworld.wolfram.com/DihedralGroup.html
''' You can use this code in Excel, Access, etc...
''' </summary>
''' <remarks></remarks>
'The multiplication table
Dim d(0 To 9) As Variant
'The permutation table
Dim p(0 To 8) As Variant
'The inverse table
Dim inv(0 To 9) As Integer
Private Sub initVerhoeffConsts()
If IsArray(d(0)) Then Exit Sub 'Shortcut if already initiated
d(0) = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
d(1) = Array(1, 2, 3, 4, 0, 6, 7, 8, 9, 5)
d(2) = Array(2, 3, 4, 0, 1, 7, 8, 9, 5, 6)
d(3) = Array(3, 4, 0, 1, 2, 8, 9, 5, 6, 7)
d(4) = Array(4, 0, 1, 2, 3, 9, 5, 6, 7, 8)
d(5) = Array(5, 9, 8, 7, 6, 0, 4, 3, 2, 1)
d(6) = Array(6, 5, 9, 8, 7, 1, 0, 4, 3, 2)
d(7) = Array(7, 6, 5, 9, 8, 2, 1, 0, 4, 3)
d(8) = Array(8, 7, 6, 5, 9, 3, 2, 1, 0, 4)
d(9) = Array(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
p(0) = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
p(1) = Array(1, 5, 7, 6, 2, 8, 3, 0, 9, 4)
p(2) = Array(5, 8, 0, 3, 7, 9, 6, 1, 4, 2)
p(3) = Array(8, 9, 1, 6, 0, 4, 3, 5, 2, 7)
p(4) = Array(9, 4, 5, 3, 1, 2, 6, 8, 7, 0)
p(5) = Array(4, 2, 8, 6, 5, 7, 3, 9, 0, 1)
p(6) = Array(2, 7, 9, 3, 8, 0, 6, 4, 1, 5)
p(7) = Array(7, 0, 4, 6, 9, 1, 3, 2, 5, 8)
inv(0) = 0: inv(1) = 4: inv(2) = 3: inv(3) = 2: inv(4) = 1
inv(5) = 5: inv(6) = 6: inv(7) = 7: inv(8) = 8: inv(9) = 9
End Sub
''' <summary>
''' Validates that an entered number is Verhoeff compliant.
''' </summary>
''' <param name="num"></param>
''' <returns>True if Verhoeff compliant, otherwise false</returns>
''' <remarks>Make sure the check digit is the last one!</remarks>
Public Function validateVerhoeff(ByVal num As String) As Boolean
initVerhoeffConsts
Dim c As Integer
Dim i As Integer
c = 0
Dim myArray() As Integer
myArray = StringToReversedIntArray(num)
For i = 0 To UBound(myArray)
c = d(c)(p((i Mod 8))(myArray(i))) 'Version corrected by WHG gives error
Next i
validateVerhoeff = (c = 0)
End Function
''' <summary>
''' For a given number generates a Verhoeff digit
''' </summary>
''' <param name="num"></param>
''' <returns>Verhoeff check digit as Integer</returns>
''' <remarks>Append this check digit to num</remarks>
Public Function generateVerhoeff(ByVal num As String) As Integer
initVerhoeffConsts
Dim c As Integer
Dim i As Integer
c = 0
Dim myArray() As Integer
myArray = StringToReversedIntArray(num)
For i = 0 To UBound(myArray)
c = d(c)(p((i + 1) Mod 8)(myArray(i))) 'Version corrected by WHG gives error in compilation
Next i
generateVerhoeff = inv(c) 'str(inv(c))
End Function
''' <summary>
''' Converts a string to a reversed integer array.
''' </summary>
''' <param name="str"></param>
''' <returns>Reversed integer array</returns>
''' <remarks></remarks>
Private Function StringToReversedIntArray(ByVal str As String) As Integer()
Dim lg As Integer
lg = Len(str)
Dim myArray() As Integer
ReDim myArray(0 To lg - 1)
Dim i As Integer
For i = 0 To lg - 1
myArray(i) = AscW(Mid$(str, lg - i, 1)) - AscW("0")
Next
StringToReversedIntArray = myArray
End Function
''' In Excel don't copy this sub _AssertsVerhoeff()as get a compilation error. 4/21/2013
Public Sub _AssertsVerhoeff()
Debug.Print "Start Verhoeff's Asserts"
Debug.Assert generateVerhoeff("75872") = 2
Debug.Assert validateVerhoeff("758722") = True
Debug.Assert generateVerhoeff("12345") = 1
Debug.Assert validateVerhoeff("123451") = True
Debug.Assert generateVerhoeff("142857") = 0
Debug.Assert validateVerhoeff("1428570") = True
Debug.Assert generateVerhoeff("123456789012") = 0
Debug.Assert validateVerhoeff("1234567890120") = True
Debug.Assert generateVerhoeff("8473643095483728456789") = 2
Debug.Assert validateVerhoeff("84736430954837284567892") = True
Debug.Assert generateVerhoeff("12345") = 1
Debug.Assert validateVerhoeff("123451") = True
Debug.Assert validateVerhoeff("124351") = False
Debug.Assert validateVerhoeff("122451") = False
Debug.Assert validateVerhoeff("128451") = False
Debug.Assert validateVerhoeff("214315") = False
Debug.Print "End Verhoeff's Asserts"
End Sub

VB.NET - How to calculate the parity bit of a byte array

What is the most efficient way calculate the parity bit (if the number of active bits are odd or even) in a byte array? I have though about iterating through all the bits and summing up the active bits, but that would be very impractical purely based on the number of iterations required on larger byte arrays/files.
For your convenience (and my curiosity), I have done some timing tests with a parity lookup table compared to the other two methods suggested so far:
Module Module1
Dim rand As New Random
Dim parityLookup(255) As Integer
Sub SetUpParityLookup()
' setBitsCount data from http://stackoverflow.com/questions/109023/how-to-count-the-number-of-set-bits-in-a-32-bit-integer
Dim setBitsCount = {
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
}
For i = 0 To 255
parityLookup(i) = setBitsCount(i) And 1
Next
End Sub
' Method using lookup table
Function ParityOfArray(a() As Byte) As Integer
Dim parity As Integer = 0 ' use an Integer because they are faster
For i = 0 To a.Length - 1
parity = parity Xor parityLookup(a(i))
Next
Return parity
End Function
' Method by Alireza
Function ComputeParity(bytes() As Byte) As Byte
Dim parity As Boolean = False
For i As Integer = 0 To bytes.Length - 1
Dim b As Byte = bytes(i)
While b <> 0
parity = Not parity
b = CByte(b And (b - 1))
End While
Next
Return Convert.ToByte(parity)
End Function
' Method by dbasnett
Function CountBits(byteArray As Byte()) As Integer
Dim rv As Integer = 0
For Each b As Byte In byteArray
Dim count As Integer = b
count = ((count >> 1) And &H55) + (count And &H55)
count = ((count >> 2) And &H33) + (count And &H33)
count = ((count >> 4) And &HF) + (count And &HF)
rv += count
Next
Return rv
End Function
Sub FillWithRandomBytes(ByRef a() As Byte)
rand.NextBytes(a)
End Sub
Sub Main()
SetUpParityLookup()
Dim nBytes = 10000
Dim a(nBytes - 1) As Byte
FillWithRandomBytes(a)
Dim p As Integer
Dim sw As New Stopwatch
sw.Start()
p = ParityOfArray(a)
sw.Stop()
Console.WriteLine("ParityOfArray - Parity: {0} Time: {1}", p, sw.ElapsedTicks)
sw.Restart()
p = ComputeParity(a)
sw.Stop()
Console.WriteLine("ComputeParity - Parity: {0} Time: {1}", p, sw.ElapsedTicks)
sw.Restart()
p = CountBits(a)
sw.Stop()
' Note that the value returned from CountBits should be And-ed with 1.
Console.WriteLine("CountBits - Parity: {0} Time: {1}", p And 1, sw.ElapsedTicks)
Console.ReadLine()
End Sub
End Module
Typical ouput:
ParityOfArray - Parity: 0 Time: 386
ComputeParity - Parity: 0 Time: 1014
CountBits - Parity: 0 Time: 695
An efficient way to do this is to use the x & (x - 1) operation in a loop, until x becomes zero. This way you will loop only by the number of bits set to 1.
In VB.NET for a byte array:
Function ComputeParity(bytes() As Byte) As Byte
Dim parity As Boolean = False
For i As Integer = 0 To bytes.Length - 1
Dim b As Byte = bytes(i)
While b <> 0
parity = Not parity
b = b And (b - 1)
End While
Next
Return Convert.ToByte(parity)
End Function
Here is a function that counts bits.
Private Function CountBits(byteArray As Byte()) As Integer
Dim rv As Integer = 0
For x As Integer = 0 To byteArray.Length - 1
Dim b As Byte = byteArray(x)
Dim count As Integer = b
count = ((count >> 1) And &H55) + (count And &H55)
count = ((count >> 2) And &H33) + (count And &H33)
count = ((count >> 4) And &HF) + (count And &HF)
rv += count
Next
Return rv
End Function
Note: this code came from a collection of bit twiddling hacks I found some years ago. I converted it to VB.

Comparing two arrays in vb.net

I need to do Reverse Intersection operation with two arrays and save the result in a different array
Eg: Array A {1, 2, 3}; Array B {1, 2, 3, 4, 5, 6} Resultant Array Should be {4, 5, 6}
I Tried out the following logic but didn't work
int k = 0;
int a[2] = {1,10};
int p[10];
int roll[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i = 0; i < 2; i++)
{
for (int j = 1; j <= 10; j++)
{
if (a[i] == roll[j])
{
break;
}
else
{
p[k] = 0;
p[k] = roll[j];
k++;
}
}
}
I need it for my vb.net project
I don't understand how that C# code is related to your VB.NET problem, if you only want to find integers which are in one array and not in the other, use Enumerable.Except:
Dim intsA = {1, 2, 3}
Dim intsB = {1, 2, 3, 4, 5, 6}
Dim bOnly = intsB.Except(intsA).ToArray()
Try something like this if you can't use Linq:
Function RevIntersect(arr1() As String, arr2() As String) As String()
Dim sResult, aux As New List(Of String)()
aux.AddRange(arr1)
aux.AddRange(arr2)
For Each elem As String In aux
If (Not arr1.Contains(elem) OrElse Not Arr2.Contains(elem)) AndAlso _
Not sResult.Contains(elem) Then
sResult.Add(elem)
End If
Next
Return sResult.ToArray()
End Function

String to double array

I need to take this string:
Dim tmpTry As String = "10, 20, 30, 40, 50, 52, 20, 20, 10, 35, 3, 8, 47, 7, 2, 5, 55, 8, 0, 0, 6, 55, 0, 2, 12, 0, 0, 21, 14, 0, 3"
And convert it to a double array:
Dim arrNumOfVisits As Double() = New Double(tmpTry) {}
How do i go about doing that?
FYI the arrNumOfVisits goes into a ParamArray System.Collections.IEnumerable()
David
Dim arrString As String() = tmpTry.Split(New Char() {" "C})
Dim arrNumOfVisits As Double() = New Double(arrString.Length) {}
Dim i As Integer = 0
While i < arrString.Length
arrNumOfVisits(i) = Double.Parse(arrString(i))
i += 1
End While
The above code will do the trick, using regEx on this would be overkill.
Never the less do try to learn the basic RegEx operations, here are my favorite cheat sheets:
http://regexlib.com/CheatSheet.aspx?AspxAutoDetectCookieSupport=1
http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/