Reading enum with hexademinal - vb.net

Hi can someone give me a little help, this is my Enum code
Enum _WSC_SECURITY_PROVIDER
WSC_SECURITY_PROVIDER_FIREWALL = &H1
WSC_SECURITY_PROVIDER_AUTOUPDATE_SETTINGS = &H2
WSC_SECURITY_PROVIDER_ANTIVIRUS = &H4
WSC_SECURITY_PROVIDER_ANTISPYWARE = &H8
WSC_SECURITY_PROVIDER_INTERNET_SETTINGS = &H16
WSC_SECURITY_PROVIDER_USER_ACCOUNT_CONTROL = &H32
WSC_SECURITY_PROVIDER_SERVICE = &H64
WSC_SECURITY_PROVIDER_NONE = &H0
End Enum
Enum _WSC_SCANNER_SETTINGS
SCANNER_UNKNOWN = &H1
SCANNER_RUNNING = &H16
End Enum
Enum _WSC_UPTODATE
up_To_Date = &H0
too_Old = &H10
End Enum
So I heve this Hexadecimal value: 61100 that in Binary is: 1100001000100000000, and I want that my output be
WSC_SECURITY_PROVIDER_ANTIVIRUS + WSC_SECURITY_PROVIDER_AUTOUPDATE_SETTINGS + ???? + up_To_Date
Until now I only have this but it doesn't do what I want
Dim _security As _WSC_SECURITY_PROVIDER = hex.Substring(0, 2)
Dim _scanner As _WSC_SCANNER_SETTINGS = hex.Substring(2, 2)
Dim _uptodate As _WSC_UPTODATE = hex.Substring(4, 2)
Console.WriteLine("_security={0}, _scanner={1}, _uptodate={2}",
_security,
_scanner,
_uptodate
)
I will appreciate the hints.

If you are designing an enum that is meant to represent flags, individual bits, the items in the enum should be powers of two, hex 16 is three bits. I created this enum as a template so I wouldn't have to do a lot of typing. Just change the names.
<FlagsAttribute()> _
Public Enum bit As Integer
none = 0
s0 = 1 << 0
s1 = 1 << 1
s2 = 1 << 2
s3 = 1 << 3
s4 = 1 << 4
s5 = 1 << 5
s6 = 1 << 6
s7 = 1 << 7
s8 = 1 << 8
s9 = 1 << 9
s10 = 1 << 10
s11 = 1 << 11
s12 = 1 << 12
s13 = 1 << 13
s14 = 1 << 14
s15 = 1 << 15
s16 = 1 << 16
s17 = 1 << 17
s18 = 1 << 18
s19 = 1 << 19
s20 = 1 << 20
s21 = 1 << 21
s22 = 1 << 22
s23 = 1 << 23
s24 = 1 << 24
s25 = 1 << 25
s26 = 1 << 26
s27 = 1 << 27
s28 = 1 << 28
s29 = 1 << 29
s30 = 1 << 30
all = -1
End Enum
Normally I comment out unused items, but leave them.

Related

How can I write enums with inline calculation of the powers?

I have once seen a beautifully and easy to follow enum.
It was written in such a way that one didn't have to calculate the powers by himself (1, 2, 4, 8, 16, 32, 64, 128, etc.), but instead, one could simply write like 1, 2, 3, 4, 5, etc.
The following is surely not correct, but you can get the flavor:
Public Enum eSomething
NotDefined = 0 ^ 2
Max = 1 ^ 2
Jeff = 2 ^ 2
Lisa = 3 ^ 2
Donald = 4 ^ 2
etc...
Does anybody know how to write it correctly?
Thank you very much!
Does this work for you?
Public Enum eSomething
NotDefined = CInt(2 ^ 0)
Max = CInt(2 ^ 1)
Jeff = CInt(2 ^ 2)
Lisa = CInt(2 ^ 3)
Donald = CInt(2 ^ 4)
End Enum
I have this enum that I find a convenient starting place...
<Flags()> Public Enum Bits As Long
none = 0
B00 = 1L << 0 ' 1
B01 = 1L << 1 ' 2
B02 = 1L << 2 ' 4
B03 = 1L << 3 ' 8
B04 = 1L << 4 ' 16
B05 = 1L << 5 ' 32
B06 = 1L << 6 ' 64
B07 = 1L << 7 ' 128
B08 = 1L << 8 ' 256
B09 = 1L << 9 ' 512
B10 = 1L << 10 ' 1,024
B11 = 1L << 11 ' 2,048
B12 = 1L << 12 ' 4,096
B13 = 1L << 13 ' 8,192
B14 = 1L << 14 ' 16,384
B15 = 1L << 15 ' 32,768
B16 = 1L << 16 ' 65,536
B17 = 1L << 17 ' 131,072
B18 = 1L << 18 ' 262,144
B19 = 1L << 19 ' 524,288
B20 = 1L << 20 ' 1,048,576
B21 = 1L << 21 ' 2,097,152
B22 = 1L << 22 ' 4,194,304
B23 = 1L << 23 ' 8,388,608
B24 = 1L << 24 ' 16,777,216
B25 = 1L << 25 ' 33,554,432
B26 = 1L << 26 ' 67,108,864
B27 = 1L << 27 ' 134,217,728
B28 = 1L << 28 ' 268,435,456
B29 = 1L << 29 ' 536,870,912
B30 = 1L << 30 ' 1,073,741,824
B31 = 1L << 31 ' 2,147,483,648
B32 = 1L << 32 ' 4,294,967,296
B33 = 1L << 33 ' 8,589,934,592
B34 = 1L << 34 ' 17,179,869,184
B35 = 1L << 35 ' 34,359,738,368
B36 = 1L << 36 ' 68,719,476,736
B37 = 1L << 37 ' 137,438,953,472
B38 = 1L << 38 ' 274,877,906,944
B39 = 1L << 39 ' 549,755,813,888
B40 = 1L << 40 ' 1,099,511,627,776
B41 = 1L << 41 ' 2,199,023,255,552
B42 = 1L << 42 ' 4,398,046,511,104
B43 = 1L << 43 ' 8,796,093,022,208
B44 = 1L << 44 ' 17,592,186,044,416
B45 = 1L << 45 ' 35,184,372,088,832
B46 = 1L << 46 ' 70,368,744,177,664
B47 = 1L << 47 ' 140,737,488,355,328
B48 = 1L << 48 ' 281,474,976,710,656
B49 = 1L << 49 ' 562,949,953,421,312
B50 = 1L << 50 ' 1,125,899,906,842,624
B51 = 1L << 51 ' 2,251,799,813,685,248
B52 = 1L << 52 ' 4,503,599,627,370,496
B53 = 1L << 53 ' 9,007,199,254,740,992
B54 = 1L << 54 ' 18,014,398,509,481,984
B55 = 1L << 55 ' 36,028,797,018,963,968
B56 = 1L << 56 ' 72,057,594,037,927,936
B57 = 1L << 57 ' 144,115,188,075,855,872
B58 = 1L << 58 ' 288,230,376,151,711,744
B59 = 1L << 59 ' 576,460,752,303,423,488
B60 = 1L << 60 ' 1,152,921,504,606,846,976
B61 = 1L << 61 ' 2,305,843,009,213,693,952
B62 = 1L << 62 ' 4,611,686,018,427,387,904
B63 = 1L << 63 ' -9,223,372,036,854,775,808
End Enum

Loop through a string to find the odd numbers VB

I am looping through a card number finding all the odd numbers and multiplying them by the card digits. Its kind of hard to explain. I am having trouble multiplying the odd number and the card number. Here's an example my teacher gave me. You multiply card number 1 and and odd number 1 and so forth. I am not getting any errors, it just kind of freezes.
Sums
Card #: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
Multiples 1 2 3 4 5 6 7 8
Evens: 2 4 6 8 0 2 4 6 32 =Sum 1
Odds: 1 6 15 28 45 6 21 40 162 =Sum 2
Sum 3: 194
194 =Sum 3
Step 4: =1+9+4 = 14
= 1 + 4 = 5 = check digit
Public Class Payment
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Dim Sum1 = 0
Dim Sum2 = 0
Dim Sum3 = 0
Dim ready As Boolean
Dim ccnumb = CardNumber.Text
Format(CardNumber.Text, "################")
Dim exp = Mid(ExpDate.Text, 1, 3)
Dim checkdigit = 0
If FullName.TextLength = 0 Or cardtype.Text.Length = 0 And ccnumb.Length <= 16 Or exp.Length = 2 Then
MessageBox.Show("Please enter all credit card information before proceeding.")
ready = False
Else ready = True
End If
If ready = True Then
For Each num As Char In ccnumb
If CInt(CStr(num)) Mod 2 <> 0 Then
Sum1 += CInt(CStr(num)) * CInt(CStr(num)) Mod 2 <> 0
Else
Sum2 += CInt(CStr(num))
End If
Next
Sum3 = Sum1 + Sum2
Do While Sum3 > 10
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
Do While exp.Length > 1
checkdigit = Mid(ExpDate.Text, 1, 1) + Mid(ExpDate.Text, 1, 2)
Loop
If Sum3 = checkdigit Then
MessageBox.Show("Congratulations! Your payment was successful.")
CustInv.Show()
Else MessageBox.Show("The checkdigit," & Space(1) & Sum3 & Space(1) & "does not match the month code," & Space(1) & checkdigit & "." & Space(1) & "Please reenter your card information.")
End If
End If
End Sub
"it kind of freezes" is lay speak for "my code enters an infinite loop".
This looks suspicious:
Do While Sum3 > 10
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop
To enter the loop, Sum3 must be greater than 10. For the loop to exit, Sum3 must not be greater than 10, but your code only increments Sum3, so Sum3 can only stay greater than 10.
This means that once entered, this loop is infinite.
If changing outer loop to inside loop doesn't help you
Try this
Do While Sum3 > 10 and j < sum3 ' adding j < sum3 might stop the loop
For j = 0 To Sum3.ToString.Length - 1
For k = 1 To Sum3.ToString.Length - 1
Sum3 = j + k
Next
Next
Loop

EXPLAIN what this VB CODE means

Function convertToText(ByVal data As String) As String
Dim result As String = Nothing
Dim i As Integer = 0
Dim j As Integer = 0
For Each c As Char In data.ToCharArray
j *= 2
If c = "1"c Then j += 1
i += 1
If i = 8 Then
i = 0
result &= Chr(j)
j = 0
End If
Next
Return result
End Function
It converts binary to text but its a bit difficult for me to understand the logic behind it.
Someone please help.
The code seems to convert a text containing a binary number representing 8 bit character codes to a string containing these characters.
The for each loop loops over all binary digits ("0" or "1") of the input. The code of each result character is computed and after every 8 input characters the code is considered to be complete and the new character whose code was determined is added to the result (result &= Chr(j) is the same as result = result & Chr(j). Chr(j) converts an Integer containing a character code into a character). The variable i counts the bits.
The variable j holds the character code. If a bit is "1", then 1 is added to j (j += 1 is the same as j = j + 1), but not if it is "0".
A "1" in the right most bit position has a (decimal) value of 1. The next to its left a value of 2. The next 4 and so on. The value doubles for each position until it reaches 128 for the left most bit of an 8 bit number. Therefore j is doubled on each loop (j *= 2 is the same as j = j * 2).
Example with just 4 bits:
data = "1010"
The binary number 1010 means
1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = (decimal)10
The code does this
j = 0 => 0
j *= 2 => 0
j += 1 => 1 'since c = "1"
j *= 2 => 2
'no += 1 since c = "0"
j *= 2 => 4
j += 1 => 5 'since c = "1"
j *= 2 => 10
'no += 1 since c = "0"
The first 1 we added is doubled 3 times and becomes 8. The second 1 we added is doubled only once and becomes 2. 8 + 2 = 10.

project euler problem17 vb code bug

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
my answer is coming 21148. i cant find the mistake here is my code in vb
the answer should be 21124
Dim length As UInt32 = 11
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 999
If i / 10 < 1 Then
single_digit(i)
ElseIf i / 10 = 1 Then
ten()
ElseIf i / 10 > 1 And i / 10 < 2 Then
ele_twe_thi(i)
ElseIf i / 10 >= 2 And i / 10 < 10 Then
multiple_10(Math.Floor(i / 10) * 10)
single_digit(i Mod 10)
ElseIf i = 100 Then
single_digit(i / 100)
length += 7
ElseIf i Mod 100 >= 11 And i Mod 100 <= 19 Then
length += 10
single_digit(Math.Floor(i / 100))
ele_twe_thi(i Mod 100)
ElseIf i Mod 100 = 10 Then
length += 3
single_digit(Math.Floor(i / 100))
length += 10
Else
length += 10
single_digit(Math.Floor(i / 100))
multiple_10(Math.Floor((i Mod 100) / 10) * 10)
single_digit(i Mod 10)
End If
Next
MsgBox(length)
End Sub
Private Sub single_digit(num)
If num = 1 Then
length = length + 3
ElseIf num = 2 Then
length = length + 3
ElseIf num = 3 Then
length = length + 5
ElseIf num = 4 Then
length = length + 4
ElseIf num = 5 Then
length = length + 4
ElseIf num = 6 Then
length = length + 3
ElseIf num = 7 Then
length = length + 5
ElseIf num = 8 Then
length = length + 5
ElseIf num = 9 Then
length = length + 4
End If
End Sub
Private Sub ele_twe_thi(num)
If num = 11 Or num = 12 Then
length = length + 6
ElseIf num = 13 Or num = 14 Or num = 18 Or num = 19 Then
length = length + 8
ElseIf num = 17 Then
length = length + 9
ElseIf num = 15 Or num = 16 Then
length = length + 7
End If
End Sub
Private Sub multiple_10(num)
If num = 20 Then
length = length + 6
ElseIf num = 30 Then
length = length + 6
ElseIf num = 40 Then
length = length + 5
ElseIf num = 50 Then
length = length + 5
ElseIf num = 60 Then
length = length + 5
ElseIf num = 70 Then
length = length + 7
ElseIf num = 80 Then
length = length + 6
ElseIf num = 90 Then
length = length + 6
End If
End Sub
Private Sub ten()
length = length + 3
End Sub
every time the program reaches 100, 200, 300 etc. is it adding an "and" unnecessarily?

Variable and string concat to select variable in lua

I have a set of variables that holds quantity info and x to select the one I use. How can I concatenate the letter s and var x and have it read as s2 or s3 etc. The code I managed to find does not work.
x = 2
s1 = false
s2 = 64
s3 = 64
s4 = 64
s5 = 0
if s2 >= 0 then
x = 2
elseif s3 >= 0 then
x = 3
elseif s4 >= 0 then
x = 4
elseif s5 >= 0 then
x = 5
end
if turtle.placeDown() then
tryUp()
turtle.select(1)
_G["s"..x] = _G["s"..x] - 1
end
Why would you need to do that?
My suggestion to improve your code would be something like this:
local s = {false, 64, 64, 64, 0}
for i = 2, #s do
if s[i] >= 0 then
x = s[i]
end
end
if turtle.placeDown() then
tryUp()
turtle.select(1)
x = x-1
end
Using a loop makes the code somewhat neater, and there is no real need for you to use global variables. If you insist on using _G with the string concatenation with your original code, try this:
x = 2
s1 = false
s2 = 64
s3 = 64
s4 = 64
s5 = 0
if s2 >= 0 then
x = "2" --Notice the string here
elseif s3 >= 0 then
x = "3"
elseif s4 >= 0 then
x = "4"
elseif s5 >= 0 then
x = "5"
end
if turtle.placeDown() then
tryUp()
turtle.select(1)
_G["s"..x] = _G["s"..x] - 1
end
This replaces all the x values with strings instead of numbers, which was probably what was causing the error