How I can get result value from this string formula :
Dim Formula As String
Formula = "((5000 / 30) * (22 + 6)) + ((5000 / 30 / 8) * (20))"
Dim Result As Integer
Result = ?????
How can I get an integer result value? It's 5083.33
A bit simpler - use the .Compute method of the DataTable
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim dt As New DataTable
Dim r = dt.Compute("((5000 / 30) * (22 + 6)) + ((5000 / 30 / 8) * (20))", Nothing)
Debug.Print(r.ToString)
End Sub
Result in Immediate Window
5083.33333333333
Thanks every body,
I got the CODE right now as following :-
Just you Can Do ..... >>> NumericBox1 = EvaluateExpr(TextBox2.Text)
Private Function IsEmptyStack(ByVal AStack As Stack) As Boolean
Return (AStack.Count = 0)
End Function
Private Function IsOperator(ByVal AChar As Char) As Boolean
Return "^*/+-".Contains(AChar)
End Function
Private Function CompareOperators(ByVal Op1 As Char, ByVal Op2 As Char) As Integer
If Not (IsOperator(Op1) And IsOperator(Op2)) Then
Err.Raise(vbObjectError + 1001, "CompareOperators", "Operator(s) not suppoerted")
End If
Select Case Op1
Case "^"c
If Op2 = "^"c Then
Return 0
Else
Return 1
End If
Case "*"c, "/"c
Select Case Op2
Case "^"c
Return -1
Case "*"c, "/"c
Return 0
Case "+"c, "-"c
Return 1
End Select
Case "+"c, "-"c
Select Case Op2
Case "^"c, "*"c, "/"c
Return -1
Case "+"c, "-"c
Return 0
End Select
End Select
End Function
Private Function InfixToPostfix(ByVal InfixExpression As String) As String
Dim Infix As String
Dim Postfix As String
Dim InfixIndex As Integer
Dim InfixLen As Integer
Dim AChar As Char
Dim APeek As String
Dim ANumber As String
Dim MathStack As New Stack
Infix = InfixExpression.Trim()
If Infix = "" Then
Return ""
End If
Infix = Infix & ")"
ANumber = ""
Postfix = ""
InfixLen = Len(Infix) '- 1
InfixIndex = 0
MathStack.Clear()
MathStack.Push("(")
Do While (Not IsEmptyStack(MathStack)) And (InfixIndex <= InfixLen)
'AChar = Mid$(Infix, InfixIndex, 1)
AChar = Infix(InfixIndex)
If Char.IsDigit(AChar) Then
ANumber = ANumber & AChar
ElseIf AChar = "(" Then
If ANumber <> "" Then
Postfix = Postfix & ANumber & " "
ANumber = ""
End If
MathStack.Push(AChar)
ElseIf IsOperator(AChar) Then
If ANumber <> "" Then
Postfix = Postfix & ANumber & " "
ANumber = ""
End If
APeek = MathStack.Peek
If IsOperator(APeek) Then
Do While CompareOperators(APeek, AChar) >= 0
APeek = MathStack.Pop
Postfix = Postfix & APeek
APeek = MathStack.Peek
If Not IsOperator(APeek) Then Exit Do
Loop
End If
MathStack.Push(AChar)
ElseIf AChar = ")" Then
If ANumber <> "" Then
Postfix = Postfix & ANumber & " "
ANumber = ""
End If
APeek = MathStack.Peek
Do While APeek <> "("
APeek = MathStack.Pop
Postfix = Postfix & APeek
APeek = MathStack.Peek
Loop
MathStack.Pop()
End If
InfixIndex = InfixIndex + 1
Loop
If Not IsEmptyStack(MathStack) Then
Err.Raise(vbObjectError + 1002, "InfixToPostfix", "Invalid infix expression")
Else
InfixToPostfix = Postfix
End If
End Function
Private Function PerformOperation(ByVal Number1 As Double, ByVal Number2 As Double, ByVal AOperator As Char) As Double
Select Case AOperator
Case "+"c
Return Number1 + Number2
Case "-"c
Return Number1 - Number2
Case "*"c
Return Number1 * Number2
Case "/"c
If Number2 = 0 Then
Err.Raise(vbObjectError + 1004, "EvaluatePostfix", "Division by zero")
Else
Return Number1 / Number2
End If
Case "^"c
Return Number1 ^ Number2
Case Else
Err.Raise(vbObjectError + 1001, "CompareOperators", "Operator not suppoerted")
End Select
End Function
Private Function EvaluatePostfix(ByVal PostfixExpression As String) As Double
Dim Postfix As String
Dim ANumber As String
Dim AChar As Char
Dim PostfixIndex As Long
Dim PostfixLen As Long
Dim Num1 As Double
Dim Num2 As Double
Dim NumResult As Double
Dim MathStack As New Stack
Postfix = Trim$(PostfixExpression)
If Postfix = "" Then
Return 0.0
End If
Postfix = Postfix & "="
ANumber = ""
PostfixLen = Len(Postfix)
PostfixIndex = 0
MathStack.Clear()
Do While PostfixIndex <= PostfixLen
AChar = Postfix(PostfixIndex)
If AChar = " " Then
If ANumber <> "" Then
MathStack.Push(CDbl(ANumber))
ANumber = ""
End If
ElseIf Char.IsDigit(AChar) Then
ANumber = ANumber & AChar
ElseIf AChar = "=" Then
If ANumber <> "" Then
MathStack.Push(CDbl(ANumber))
ANumber = ""
End If
If MathStack.Count = 1 Then
Return MathStack.Pop
Else
Err.Raise(vbObjectError + 1003, "EvaluatePostfix", "Invalid postfix expression")
End If
ElseIf IsOperator(AChar) Then
If ANumber <> "" Then
MathStack.Push(CDbl(ANumber))
ANumber = ""
End If
If IsEmptyStack(MathStack) Then
Err.Raise(vbObjectError + 1003, "EvaluatePostfix", "Invalid postfix expression")
Else
Num2 = MathStack.Pop
If IsEmptyStack(MathStack) Then
Err.Raise(vbObjectError + 1003, "EvaluatePostfix", "Invalid postfix expression")
Else
Num1 = MathStack.Pop
NumResult = PerformOperation(Num1, Num2, AChar)
MathStack.Push(NumResult)
End If
End If
End If
PostfixIndex = PostfixIndex + 1
Loop
End Function
Public Function EvaluateExpr(ByVal AExpr As String) As String
Dim PostfixExpr As String
AExpr = AExpr.Trim
If AExpr = "" Then Return ""
PostfixExpr = InfixToPostfix(AExpr)
Return EvaluatePostfix(PostfixExpr)
End Function
This shouldn't really qualify as an answer, but I thought it would be fun to re-write that big code block using more modern techniques. I've done this right in the reply window, so there are probably several errors. It's worth noting, though, how much shorter this is.
We could have even more fun, and probably perform better, by also re-writing the code to think in terms of breaking apart the string into tokens, rather than by character.
Public Module Math
'Reverse precedence order, so higher precedence has higher index
Private operators As String = "-+/*^"
Private Function CompareOperators(Op1 As Char, Op2 As Char) As Integer
Dim Op1Value As Integer = operators.IndexOf(Op1)
Dim Op2Value As Integer = operators.IndexOf(Op2)
If Op1Value = -1 Then Throw New Exception($"Unsupported operator {Op1} detected")
If Op2Value = -1 Then Throw New Excpetion($"Unsupported operator {Op2} detected")
'The \ 2 adjusts for same precedence of +- and */
Return (Op1Value \ 2).CompareTo(Op2Value \ 2)
End Function
Private Function InfixToPostfix(InfixExpression As String) As String
If String.IsNullOrWhitesapce(InfixExpression) Then Return ""
InfixExpression = InfixExpression.Trim() & ")"
Dim result As New StringBuilder()
Dim MathStack As New Stack(Of Char)()
Dim ANumber As String = ""
Dim Index As Integer = 0
MathStack.Push("("c)
While MathStack.Count > 0 AndAlso Index <= InfixExpression.Length)
Dim AChar As Char = InfixExpression(Index)
If Char.IsDigit(AChar) Then
ANumber &= ANumber & AChar
ElseIf AChar = "("c Then
If Not String.IsNullOrEmpty(ANumber) Then
result.Append(ANumber).Append(" ")
ANumber = ""
End If
MathStack.Push(AChar)
ElseIf IsOperator(AChar) Then
If Not String.IsNullOrEmpty(ANumber) Then
result.Append(ANumber).Append(" ")
ANumber = ""
End If
Dim APeek As Char = MathStack.Peek()
If IsOperator(APeek) Then
While CompareOperators(APeek, AChar) >= 0
APeek = MathStack.Pop()
result.Append(APeek)
APeek = MathStack.Peek
If Not IsOperator(APeek) Then Exit While
End While
End If
MathStack.Push(AChar)
ElseIf AChar = ")"c Then
If Not String.IsNullOrEmpty(ANumber) Then
result.Append(ANumber).Append(" ")
ANumber = ""
End If
APeek = MathStack.Peek()
While APeek <> "("c
APeek = MathStack.Pop()
result.Append(APeek)
APeek = MathStack.Peek()
End While
MathStack.Pop()
End If
Index += 1
End While
If MathStack.Count > 0 Then
Throw New Exception("Invalid infix expression: stack is not empty")
End If
Return result.ToString()
End Function
Private Function PerformOperation(Number1 As Double, Number2 As Double, AOperator As Char) As Double
Select Case AOperator
Case "+"c
Return Number1 + Number2
Case "-"c
Return Number1 - Number2
Case "*"c
Return Number1 * Number2
Case "/"c
'We could detect Number2 = 0 here, but appropriate response is throwing the same DivideByZeroException the framework will do for us anyway
Return Number1 / Number2
Case "^"c
Return Number1 ^ Number2
Case Else
Throw New Exception($"Operator {AOperator} not supported")
End Select
End Function
Private Function EvaluatePostfix(Expression As String) As Double
Dim result As Double = 0R
If String.IsNullOrWhitespace(Expression) Then Return result
Expression = Expression.Trim() & "="
Dim MathStack As New Stack(Of Double)()
Dim ANumber As String = ""
Dim Index As Integer = 0
Do While Index <= Expression.Length
AChar = Expression(Index)
If AChar = " "c Then
If Not String.IsNullOrEmpty(ANumber) Then
MathStack.Push(CDbl(ANumber))
ANumber = ""
End If
ElseIf Char.IsDigit(AChar) Then
ANumber = ANumber & AChar
ElseIf AChar = "="c Then
If Not String.IsNullOrEmpty(ANumber) Then
MathStack.Push(CDbl(ANumber))
ANumber = ""
End If
If MathStack.Count = 1 Then Return MathStack.Pop()
Throw New Exception("Invalid postfix expression")
ElseIf IsOperator(AChar) Then
If Not String.IsNullOrEmpty(ANumber) Then
MathStack.Push(CDbl(ANumber))
ANumber = ""
End If
If MathStack.Count < 2 Then
Throw New Exception("Invalid postfix expression: insufficient stack")
End If
Dim Num2 As Double = MathStack.Pop()
MathStack.Push(PerformOperation(MathStack.Pop(), Num2))
End If
Index += 1
Loop
End Function
Public Function EvaluateExpr(AExpr As String) As String
If String.IsNullOrWhitespacE(AExpr) Then Return ""
Return EvaluatePostfix(InfixToPostfix(AExpr))
End Function
End Module
Wanted to do the token option for more fun:
Public Module Math
'Reverse precedence order, so higher precedence has higher index
Private operators As String = "()-+/*^"
Private Function IsOperator(AChar As Char) As Boolean
Return operators.Contains(AChar)
End Function
Private Function CompareOperators(Op1 As Char, Op2 As Char) As Integer
Dim Op1Value As Integer = operators.IndexOf(Op1)
Dim Op2Value As Integer = operators.IndexOf(Op2)
If Op1Value = -1 Then Throw New Exception($"Unsupported operator '{Op1}' detected")
If Op2Value = -1 Then Throw New Exception($"Unsupported operator '{Op2}' detected")
'The \ 2 adjusts for same precedence of +- and */
Return (Op1Value \ 2).CompareTo(Op2Value \ 2)
End Function
Private Iterator Function Tokenize(input As String) As IEnumerable(Of String)
Dim buffer As String = ""
For Each c As Char In input
If Char.IsWhitespace(c) Then
If String.IsNullOrEmpty(buffer) Then Continue For
Yield buffer
buffer = ""
ElseIf Char.IsDigit(c) OrElse c = "."c Then
buffer &= c 'Don't worry about validating good numbers at this level. Just check the characters
ElseIf c = "-"c Then ' could be operator or negative sign
If buffer.Length > 0 Then 'was an operator
Yield buffer
buffer = ""
Yield c.ToString()
Else 'Not sure yet -- treat as digit for now
buffer &= c
End If
ElseIf operators.Contains(c) OrElse "()".Contains(c) Then
If buffer.Length > 0 Then
Yield buffer
buffer = ""
End If
Yield c.ToString()
Else
Throw New Exception($"Unexpected character '{c}' in input")
End If
Next c
If buffer.Length > 0 Then Yield buffer
End Function
Private Iterator Function InfixToPostfix(tokens As IEnumerable(Of String)) As IEnumerable(Of String)
Dim buffer As New Stack(Of String)()
Dim temp As Double
For Each token As String In tokens
If Double.TryParse(token, temp) Then
Yield token
'Need to account for "(" better
ElseIf token = "(" Then
buffer.Push(token)
ElseIf operators.Contains(token) AndAlso token <> ")" Then
If buffer.Count = 0 Then
buffer.Push(token)
ElseIf CompareOperators(token, buffer.Peek()) > 0 Then
buffer.Push(token)
Else
While CompareOperators(token, buffer.Peek()) <= 0
Dim tok As String = buffer.Pop()
If Not "()".Contains(tok) Then Yield tok
If buffer.Count = 0 Then Exit While
End While
buffer.Push(token)
End If
ElseIf token = ")" Then
Dim valid As Boolean = False
While buffer.Count > 0
Dim tok As String = buffer.Pop()
If tok = "(" Then
valid = True
Exit While
Else
Yield tok
End If
End While
If Not valid Then Throw New Exception("Unbalanced parentheses in expression (missing matching '(' character)")
Else
Throw New Exception($"Unknown token type '{token}'")
End If
Next token
While buffer.Count > 0
Dim tok As String = buffer.Pop()
If Not "()".Contains(tok) Then Yield tok
End While
End Function
Private Function PerformOperation(Number1 As Double, Number2 As Double, AOperator As Char) As Double
Select Case AOperator
Case "+"c
Return Number1 + Number2
Case "-"c
Return Number1 - Number2
Case "*"c
Return Number1 * Number2
Case "/"c
'We could detect Number2 = 0 here, but appropriate response is throwing the same DivideByZeroException the framework will do for us anyway
Return Number1 / Number2
Case "^"c
Return Number1 ^ Number2
Case Else
Throw New Exception($"Operator {AOperator} not supported")
End Select
End Function
Private Function EvaluatePostfix(tokens As IEnumerable(Of String)) As Double
Dim result As Double = 0R
Dim buffer As New Stack(Of Double)()
Dim temp As Double
For Each token As String In tokens
If Double.TryParse(token, temp) Then
buffer.Push(temp)
ElseIf buffer.Count < 2 Then
Throw New Exception("Invalid postfix expression")
Else
temp = buffer.Pop()
temp = PerformOperation(buffer.Pop(), temp, token(0))
buffer.Push(temp)
End If
Next token
If buffer.Count > 1 Then Throw New Exception("Invalid expression: extra items in the buffer")
If buffer.Count = 0 Then Throw New Exception("Invalid expression: no result")
Return buffer.Pop()
End Function
Public Function Evaluate(input As String) As Double
If String.IsNullOrWhiteSpace(input) Then Return 0R
Dim tokens = Tokenize(input)
tokens = InfixToPostfix(tokens)
Return EvaluatePostfix(tokens)
End Function
End Module
Related
Is it possible to use Format function to display integers in roman numerals?
For Counter As Integer = 1 To 10
Literal1.Text &= Format(Counter, "???")
Next
This is what I found on http://www.source-code.biz/snippets/vbasic/7.htm
(originally written by Mr Christian d'Heureuse in VB)
I converted it to VB.net:
Private Function FormatRoman(ByVal n As Integer) As String
If n = 0 Then FormatRoman = "0" : Exit Function
' there is no Roman symbol for 0, but we don't want to return an empty string
Const r = "IVXLCDM" ' Roman symbols
Dim i As Integer = Math.Abs(n)
Dim s As String = ""
For p As Integer = 1 To 5 Step 2
Dim d As Integer = i Mod 10
i = i \ 10
Select Case d ' format a decimal digit
Case 0 To 3 : s = s.PadLeft(d + Len(s), Mid(r, p, 1))
Case 4 : s = Mid(r, p, 2) & s
Case 5 To 8 : s = Mid(r, p + 1, 1) & s.PadLeft(d - 5 + Len(s), Mid(r, p, 1))
Case 9 : s = Mid(r, p, 1) & Mid(r, p + 2, 1) & s
End Select
Next
s = s.PadLeft(i + Len(s), "M") ' format thousands
If n < 0 Then s = "-" & s ' insert sign if negative (non-standard)
FormatRoman = s
End Function
I hope this will help others.
Cheers - Dave.
No, there is no standard formatter for that.
If you read the Wikipedia on Roman numerals you'll find that there are multiple ways of formatting Roman Numerals. So you will have to write your own method our use the code of someone else.
I wrote this code that works perfectly up to a million.
You can use it but, please, do not make it your own.
Public NotInheritable Class BRoman
'Written by Bernardo Ravazzoni
Public Shared Function hexRoman(ByVal input As Integer) As String
Return mainROMAN(input)
End Function
Private Shared Function mainROMAN(ByVal input As Integer) As String
Dim under As Boolean = udctr(input)
Dim cifretotali As Integer = input.ToString.Length
Dim output As String = ""
Dim remaning As String = input
Dim cifracor As Integer = cifretotali
While Not cifracor = 0
output = output & coreROMAN(division(remaning, remaning), cifracor)
cifracor = cifracor - 1
End While
If under Then
output = "-" & output
End If
Return output
End Function
Private Shared Function coreROMAN(ByVal num As Integer, ByVal pos As Integer) As String
Dim output As String = ""
Debug.WriteLine(num)
Select Case num
Case 1 To 3
output = say(num, getStringFor(True, pos))
Case 4
output = getStringFor(True, pos) & getStringFor(False, pos)
Case 5 To 8
output = getStringFor(False, pos) & say(num - 5, getStringFor(True, pos))
Case 9, 10
output = say(10 - num, getStringFor(True, pos)) & getStringFor(True, pos + 1)
End Select
Return output
End Function
Private Shared Function getStringFor(ByVal first As Boolean, ByVal index As Integer) As String
Dim output As String = ""
index = index * 2
If first Then
index = index - 1
End If
output = rGetStringFor(index)
Return output
End Function
Private Shared Function rGetStringFor(ByVal index As Integer) As String
Dim output As String = ""
Dim sy As Integer
If index < 8 Then
output = rrGetStringFor(index)
Else
sy = index \ 6
output = say(sy, rrGetStringFor(8)) & rrGetStringFor(((index - 2) Mod 6) + 2) & say(sy, rrGetStringFor(9))
End If
Return output
End Function
Private Shared Function rrGetStringFor(ByVal index As Integer) As String
Dim output As String = ""
Select Case index
Case 1
output = "I"
Case 2 '8
output = "V"
Case 3 '9
output = "X"
Case 4 '10
output = "L"
Case 5 '11
output = "C"
Case 6 '12
output = "D"
Case 7 '13
output = "M"
Case 8
output = "["
Case 9
output = "]"
End Select
Return output
End Function
Private Shared Function division(ByVal inputs As String, ByRef resto As String) As Integer
resto = ""
If inputs.Length > 1 Then
resto = inputs.Substring(1)
End If
Dim output As Integer = Integer.Parse(StrReverse(inputs).Substring(inputs.Length - 1))
Return output
End Function
Public Shared Function say(ByVal index As Integer, ByVal letter As String) As String
Dim output As String = ""
While Not index = 0
output = output & letter
index = index - 1
End While
Return output
End Function
Public Shared Function udctr(ByRef num As Integer) As Boolean
Dim und As Boolean = (num < 0)
If und Then
num = 0 - num
End If
Return und
End Function
End Class
Use the function hexRoman, like this example:
msgbox(Broman.hexRoman(50))
Public Class RomanNumber
Public Shared Function FromNumber(val As Byte) As String
Return GetNumberToRoman(val)
End Function
Public Shared Function FromNumber(val As SByte) As String
Return GetNumberToRoman(val)
End Function
Public Shared Function FromNumber(val As Int16) As String
Return GetNumberToRoman(val)
End Function
Public Shared Function FromNumber(val As Int32) As String
Return GetNumberToRoman(val)
End Function
Public Shared Function FromNumber(val As UInt16) As String
Return GetNumberToRoman(val)
End Function
Public Shared Function FromNumber(val As UInt32) As String
Return GetNumberToRoman(val)
End Function
Public Shared Function ToByte(val As String) As Byte
Return GetNumberFromRoman(val)
End Function
Public Shared Function ToSByte(val As String) As SByte
Return GetNumberFromRoman(val)
End Function
Public Shared Function ToInt16(val As String) As Int16
Return GetNumberFromRoman(val)
End Function
Public Shared Function ToInt32(val As String) As Int32
Return GetNumberFromRoman(val)
End Function
Public Shared Function ToUInt16(val As String) As UInt16
Return GetNumberFromRoman(val)
End Function
Public Shared Function ToUInt32(val As String) As UInt32
Return GetNumberFromRoman(val)
End Function
Private Shared Function GetNumberToRoman(val As Integer) As String
Dim v As String = ""
Do While val > 0
If val >= 1000 Then
v &= "M" : val -= 1000
ElseIf val >= 900 Then
v &= "CM" : val -= 900
ElseIf val >= 500 Then
v &= "D" : val -= 500
ElseIf val >= 400 Then
v &= "CD" : val -= 400
ElseIf val >= 100 Then
v &= "C" : val -= 100
ElseIf val >= 90 Then
v &= "XC" : val -= 90
ElseIf val >= 50 Then
v &= "L" : val -= 50
ElseIf val >= 40 Then
v &= "XL" : val -= 40
ElseIf val >= 10 Then
v &= "X" : val -= 10
ElseIf val >= 9 Then
v &= "IX" : val -= 9
ElseIf val >= 5 Then
v &= "V" : val -= 5
ElseIf val >= 4 Then
v &= "IV" : val -= 4
Else
v &= "I" : val -= 1
End If
Loop
Return v
End Function
Private Shared Function GetNumberFromRoman(val As String) As Object
Dim v As Integer = 0
If val.Contains("IV") Then v += 4 : val = val.Replace("IV", "")
If val.Contains("IX") Then v += 9 : val = val.Replace("IX", "")
If val.Contains("XL") Then v += 40 : val = val.Replace("XL", "")
If val.Contains("XC") Then v += 90 : val = val.Replace("XC", "")
If val.Contains("CD") Then v += 400 : val = val.Replace("CD", "")
If val.Contains("CM") Then v += 900 : val = val.Replace("CM", "")
For Each c As Char In val
If c = "I" Then v += 1
If c = "V" Then v += 5
If c = "X" Then v += 10
If c = "L" Then v += 50
If c = "C" Then v += 100
If c = "D" Then v += 500
If c = "M" Then v += 1000
Next
Return v
End Function
End Class
I have a report which carries a barcode , at the time of the Visual Studio preview it looks perfectly but when published to the server reporting services ( WEB ) does not show me the barcode as if hidden
This is the code I use within the report :
Public Shared Function Code39(ByVal stringText As String) As Byte()
Dim result As Byte() = Nothing
Try
result = GenerateImage("Code 3 de 9", StringToBarcode39String(stringText))
Catch ex As Exception
End Try
Return result
End Function
Public Shared Function Code128(ByVal stringText As String) As Byte()
Dim result As Byte() = Nothing
Try
result = GenerateImage("Code 128", StringToBarcode128String(stringText))
Catch ex As Exception
End Try
Return result
End Function
Public Shared Function GenerateImage(ByVal fontName As String, ByVal stringText As String) As Byte()
Dim oGraphics As System.Drawing.Graphics
Dim barcodeSize As System.Drawing.SizeF
Dim ms As System.IO.MemoryStream
Using font As New System.Drawing.Font(New System.Drawing.FontFamily(fontName), 36)
Using tmpBitmap As New System.Drawing.Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
oGraphics = System.Drawing.Graphics.FromImage(tmpBitmap)
oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel
barcodeSize = oGraphics.MeasureString(stringText, font)
oGraphics.Dispose()
End Using
Using newBitmap As New System.Drawing.Bitmap(barcodeSize.Width, barcodeSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
oGraphics = System.Drawing.Graphics.FromImage(newBitmap)
oGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel
Using oSolidBrushWhite As New System.Drawing.SolidBrush(System.Drawing.Color.White)
Using oSolidBrushBlack As New System.Drawing.SolidBrush(System.Drawing.Color.Black)
oGraphics.FillRectangle(oSolidBrushWhite, New System.Drawing.Rectangle(0, 0, barcodeSize.Width, barcodeSize.Height))
oGraphics.DrawString(stringText, font, oSolidBrushBlack, 0, 0)
End Using
End Using
ms = New System.IO.MemoryStream()
newBitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
End Using
End Using
Return ms.ToArray()
End Function
Public Shared Function StringToBarcode128String(ByVal value As String) As String
' Parameters : a string
' Return : a string which give the bar code when it is dispayed with CODE128.TTF font
' : an empty string if the supplied parameter is no good
Dim charPos As Integer, minCharPos As Integer
Dim currentChar As Integer, checksum As Integer
Dim isTableB As Boolean = True, isValid As Boolean = True
Dim returnValue As String = String.Empty
If value.Length > 0 Then
' Check for valid characters
For charCount As Integer = 0 To value.Length - 1
'currentChar = char.GetNumericValue(value, charPos);
currentChar = AscW(Char.Parse(value.Substring(charCount, 1)))
If Not (currentChar >= 32 AndAlso currentChar <= 126) Then
isValid = False
Exit For
End If
Next
' Barcode is full of ascii characters, we can now process it
If isValid Then
charPos = 0
While charPos < value.Length
If isTableB Then
' See if interesting to switch to table C
' yes for 4 digits at start or end, else if 6 digits
If charPos = 0 OrElse charPos + 4 = value.Length Then
minCharPos = 4
Else
minCharPos = 6
End If
minCharPos = IsNumber(value, charPos, minCharPos)
If minCharPos < 0 Then
' Choice table C
If charPos = 0 Then
' Starting with table C
' char.ConvertFromUtf32(210);
returnValue = (ChrW(210)).ToString()
Else
' Switch to table C
returnValue = returnValue & (ChrW(204)).ToString()
End If
isTableB = False
Else
If charPos = 0 Then
' Starting with table B
' char.ConvertFromUtf32(209);
returnValue = (ChrW(209)).ToString()
End If
End If
End If
If Not isTableB Then
' We are on table C, try to process 2 digits
minCharPos = 2
minCharPos = IsNumber(value, charPos, minCharPos)
If minCharPos < 0 Then
' OK for 2 digits, process it
currentChar = Integer.Parse(value.Substring(charPos, 2))
currentChar = IIf(currentChar < 95, currentChar + 32, currentChar + 105) ''
returnValue = returnValue & (ChrW(currentChar)).ToString()
charPos += 2
Else
' We haven't 2 digits, switch to table B
returnValue = returnValue & (ChrW(205)).ToString()
isTableB = True
End If
End If
If isTableB Then
' Process 1 digit with table B
returnValue = returnValue & value.Substring(charPos, 1)
charPos += 1
End If
End While
' Calculation of the checksum
checksum = 0
For [loop] As Integer = 0 To returnValue.Length - 1
currentChar = AscW(Char.Parse(returnValue.Substring([loop], 1)))
currentChar = IIf(currentChar < 127, currentChar - 32, currentChar - 105)
If [loop] = 0 Then
checksum = currentChar
Else
checksum = (checksum + ([loop] * currentChar)) Mod 103
End If
Next
' Calculation of the checksum ASCII code
checksum = IIf(checksum < 95, checksum + 32, checksum + 105)
' Add the checksum and the STOP
returnValue = returnValue & (ChrW(checksum)).ToString() & (ChrW(211)).ToString()
End If
End If
Return returnValue
End Function
Private Shared Function IsNumber(ByVal InputValue As String, ByVal CharPos As Integer, ByVal MinCharPos As Integer) As Integer
' if the MinCharPos characters from CharPos are numeric, then MinCharPos = -1
MinCharPos -= 1
If CharPos + MinCharPos < InputValue.Length Then
While MinCharPos >= 0
If AscW(Char.Parse(InputValue.Substring(CharPos + MinCharPos, 1))) < 48 OrElse AscW(Char.Parse(InputValue.Substring(CharPos + MinCharPos, 1))) > 57 Then
Exit While
End If
MinCharPos -= 1
End While
End If
Return MinCharPos
End Function
Public Shared Function StringToBarcode39String(ByVal value As String, Optional ByVal addChecksum As Boolean = False) As String
' Parameters : a string
' Return : a string which give the bar code when it is dispayed with CODE128.TTF font
' : an empty string if the supplied parameter is no good
Dim isValid As Boolean = True
Dim currentChar As Char
Dim returnValue As String = String.Empty
Dim checksum As Integer = 0
If value.Length > 0 Then
'Check for valid characters
For CharPos As Integer = 0 To value.Length - 1
currentChar = Char.Parse(value.Substring(CharPos, 1))
If Not ((currentChar >= "0"c AndAlso currentChar <= "9"c) OrElse (currentChar >= "A"c AndAlso currentChar <= "Z"c) OrElse currentChar = " "c OrElse currentChar = "-"c OrElse currentChar = "."c OrElse currentChar = "$"c OrElse currentChar = "/"c OrElse currentChar = "+"c OrElse currentChar = "%"c) Then
isValid = False
Exit For
End If
Next
If isValid Then
' Add start char
returnValue = "*"
' Add other chars, and calc checksum
For CharPos As Integer = 0 To value.Length - 1
currentChar = Char.Parse(value.Substring(CharPos, 1))
returnValue += currentChar.ToString()
If currentChar >= "0"c AndAlso currentChar <= "9"c Then
checksum = checksum + AscW(currentChar) - 48
ElseIf currentChar >= "A"c AndAlso currentChar <= "Z"c Then
checksum = checksum + AscW(currentChar) - 55
Else
Select Case currentChar
Case "-"c
checksum = checksum + AscW(currentChar) - 9
Exit Select
Case "."c
checksum = checksum + AscW(currentChar) - 9
Exit Select
Case "$"c
checksum = checksum + AscW(currentChar) + 3
Exit Select
Case "/"c
checksum = checksum + AscW(currentChar) - 7
Exit Select
Case "+"c
checksum = checksum + AscW(currentChar) - 2
Exit Select
Case "%"c
checksum = checksum + AscW(currentChar) + 5
Exit Select
Case " "c
checksum = checksum + AscW(currentChar) + 6
Exit Select
End Select
End If
Next
' Calculation of the checksum ASCII code
If addChecksum Then
checksum = checksum Mod 43
If checksum >= 0 AndAlso checksum <= 9 Then
returnValue += (ChrW(checksum + 48)).ToString()
ElseIf checksum >= 10 AndAlso checksum <= 35 Then
returnValue += (ChrW(checksum + 55)).ToString()
Else
Select Case checksum
Case 36
returnValue += "-"
Exit Select
Case 37
returnValue += "."
Exit Select
Case 38
returnValue += " "
Exit Select
Case 39
returnValue += "$"
Exit Select
Case 40
returnValue += "/"
Exit Select
Case 41
returnValue += "+"
Exit Select
Case 42
returnValue += "%"
Exit Select
End Select
End If
End If
' Add stop char
returnValue += "*"
End If
End If
Return returnValue
End Function
Do I use assemblies , And the barcode image is kind
Could it be that on the server the barcode font is missing?
The Condition ( less than zero ) is not working in the below code :
Protected Sub txt_business_revenue_risk_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txt_business_revenue_risk.TextChanged
Dim a As double= 0.0
If IsNumeric(Me.txt_business_revenue_risk.Text) Then
Me.lbl_business_revenue_risk.Text = ""
Me.lbl_rest_risk.Text = CDbl(Me.txt_business_revenue_risk.Text) - CDbl(Me.txt_mitigated_risk.Text)
If CDbl(Me.txt_business_revenue_risk.Text) < CDbl(a) Then
Me.lbl_business_revenue_risk.Text = "Must be a number greater than zero!"
txt_business_revenue_risk.Text = ""
End If
If CDbl(Me.txt_business_revenue_risk.Text) < CDbl(Me.txt_mitigated_risk.Text) Then
Me.lbl_rest_risk.Text = 0
Else
Me.lbl_rest_risk.Text = CDbl(Me.txt_business_revenue_risk.Text) - CDbl(Me.txt_mitigated_risk.Text)
End If
Else
Me.lbl_business_revenue_risk.Text = "Must be a number!"
txt_business_revenue_risk.Text = ""
End If
End Sub
Avoid the usage of VB6 methods such as IsNumeric, and those lot of Double-parsings, try this:
Private Sub txt_business_revenue_risk_TextChanged(sender As Object, e As EventArgs) _
Handles txt_business_revenue_risk.TextChanged
Dim Zero As Double = 0.0
Dim Num1 As Double, Num2 As Double
If Double.TryParse(CStr(sender.text), Num1) _
AndAlso Double.TryParse(CStr(txt_mitigated_risk.Text), Num2) Then
Me.lbl_business_revenue_risk.Text = String.Empty
Me.lbl_rest_risk.Text = CStr(Num1 - Num2)
Select Case Num1
Case Is < Zero
Me.lbl_business_revenue_risk.Text = "Must be a number greater than zero!"
sender.Text = String.Empty
Case Is < Num2
Me.lbl_rest_risk.Text = CStr(Zero)
Case Else
Me.lbl_rest_risk.Text = CStr(Num1 - Num2)
End Select
Else
Me.lbl_business_revenue_risk.Text = "Must be a number!"
txt_business_revenue_risk.Text = String.Empty
End If
End Sub
Try this -- Don't forget to add exception handling
Dim resMsg As String = ""
Dim txtRevRisk As String = Me.txt_business_revenue_risk.Text
Dim txtMitRisk As String = Me.txt_mitigated_risk.Text
Dim dRes As Double = 0
Dim a As Double = 0.0
If IsNumeric(txtRevRisk) AndAlso IsNumeric(txtMitRisk) Then
Dim dRevRisk = CDbl(txtRevRisk)
Dim dMitRisk = CDbl(txtMitRisk)
resMsg = txtRevRisk
dRes = dRevRisk - dMitRisk
If dRevRisk < a Then
resMsg = "Must be a number greater than zero!"
txtRevRisk = ""
End If
If dRevRisk < dMitRisk Then
dRes = 0
'Else
' Me.lbl_rest_risk.Text = dRevRisk - dMitRisk
End If
Else
resMsg = "Must be a number!"
txtRevRisk = ""
End If
Me.lbl_rest_risk.Text = dRes.ToString()
Me.lbl_business_revenue_risk.Text = resMsg.ToString()
Me.txt_business_revenue_risk.Text = txtRevRisk.ToString()
I have a textbox on a form where the user types some text. Each letter is assigned a different value like a = 1, b = 2, c = 3 and so forth. For example, if the user types "aa bb ccc" the output on a label should be like:
aa = 2
bb = 4
dd = 6
Total value is (12)
I was able to get the total value by looping through the textbox string, but how do I display the total for each word. This is what I have so far:
For letter_counter = 1 To word_length
letter = Mid(txtBox1.Text, letter_counter, 1)
If letter.ToUpper = "A" Then
letter_value = 1
End If
If letter.ToUpper = "B" Then
letter_value = 2
End If
If letter.ToUpper = "C" Then
letter_value = 3
End If
If letter.ToUpper = "D" Then
letter_value = 4
End If
If letter.ToUpper = "E" Then
letter_value = 5
End If
If letter.ToUpper = " " Then
letter_value = 0
End If
totalletter = totalletter + letter_value
Label1.Text = Label1.Text & letter_value & " "
txtBox2.Text = txtBox2.Text & letter_value & " "
Next letter_counter
This simple little routine should do the trick:
Private Sub CountLetters(Input As String)
Label1.Text = ""
Dim total As Integer = 0
Dim dicLetters As New Dictionary(Of Char, Integer)
dicLetters.Add("a"c, 1)
dicLetters.Add("b"c, 5)
dicLetters.Add("c"c, 7)
For Each word As String In Input.Split
Dim wordtotal As Integer = 0
For Each c As Char In word
wordtotal += dicLetters(Char.ToLower(c))
Next
total += wordtotal
'Display word totals here
Label1.Text += word.PadRight(12) + "=" + wordtotal.ToString.PadLeft(5) + vbNewLine
Next
'Display total here
Label1.Text += "Total".PadRight(12) + "=" + total.ToString.PadLeft(5)
End Sub
This should give you an idea:
Dim listOfWordValues As New List(Of Integer)
For letter_counter = 1 To word_length
letter = Mid(txtBox1.Text, letter_counter, 1)
If letter = " " Then
totalletter= totalletter + letter_value
listOfWordValues.Add(letter_value)
letter_value = 0
Else
letter_value += Asc(letter.ToUpper) - 64
End If
Next letter_counter
totalletter = totalletter + letter_value
If Not txtBox1.Text.EndsWith(" ") Then listOfWordValues.Add(letter_value)
txtBox2.Text = txtBox2.Text & string.Join(", ", listOFWordValues);
You can try something like this. Assuming txtBox1 is the string the user enters and " " (space) is the word delimiter:
Dim words As String() = txtBox1.Text.Split(New Char() {" "}, StringSplitOptions.RemoveEmptyEntries)
Dim totalValue As Integer = 0
Dim wordValue As Integer = 0
For Each word As String In words
wordValue = 0
For letter_counter = 1 To word.Length
Dim letter As String = Mid(txtBox1.Text, letter_counter, 1)
Select letter.ToUpper()
Case "A":
wordValue = wordValue + 1
Case "B":
wordValue = wordValue + 2
' And so on
End Select
Next
totalValue = toalValue + wordValue
Next
The above code first takes the entered text from the user and splits it on " " (space).
Next it sets two variables - one for the total value and one for the individual word values, and initializes them to 0.
The outer loop goes through each word in the array from the Split performed on the user entered text. At the start of this loop, it resets the wordValue counter to 0.
The inner loop goes through the current word, and totals up the values of the letter via a Select statement.
Once the inner loop exits, the total value for that word is added to the running totalValue, and the next word is evaluated.
At the end of these two loops you will have calculated the values for each word as well as the total for all the worlds.
The only thing not included in my sample is updating your label(s).
Try this ..
Dim s As String = TextBox1.Text
Dim c As String = "ABCDE"
Dim s0 As String
Dim totalletter As Integer
For x As Integer = 0 To s.Length - 1
s0 = s.Substring(x, 1).ToUpper
If c.Contains(s0) Then
totalletter += c.IndexOf(s0) + 1
End If
Next
MsgBox(totalletter)
I would solve this problem using a dictionary that maps each letter to a number.
Private Shared ReadOnly LetterValues As Dictionary(Of Char, Integer) = GetValues()
Private Shared Function GetValues() As IEnumerable(Of KeyValuePair(Of Char, Integer))
Dim values As New Dictionary(Of Char, Integer)
Dim value As Integer = 0
For Each letter As Char In "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
value += 1
values.Add(letter, value)
Next
Return values
End Function
Public Function CalculateValue(input As String) As Integer
Dim sum As Integer = 0
For Each letter As Char In input.ToUpperInvariant()
If LetterValues.ContainsKey(letter) Then
sum += LetterValues.Item(letter)
End If
Next
Return sum
End Function
Usage example:
Dim sum As Integer = 0
For Each segment As String In "aa bb ccc".Split()
Dim value = CalculateValue(segment)
Console.WriteLine("{0} = {1}", segment, value)
sum += value
Next
Console.WriteLine("Total value is {0}", sum)
' Output
' aa = 2
' bb = 4
' ccc = 9
' Total value is 15
I keep getting an Overflow on the bitwise and in this first function. I fixed the other overflows by converting from Long to Currency (still seems weird), but I can't get this And to work.
Any ideas? I'm just trying to convert some IP addresses to CIDRs and calculate some host numbers.
Option Explicit
Public Function ConvertMaskToCIDR(someIP As String, someMask As String)
Dim ipL As Variant
ipL = iPToNum(someIP)
Dim maskL As Variant
maskL = iPToNum(someMask)
maskL = CDec(maskL)
'Convert Mask to CIDR(1-30)
Dim oneBit As Variant
oneBit = 2147483648#
oneBit = CDec(oneBit)
Dim CIDR As Integer
CIDR = 0
Dim x As Integer
For x = 31 To 0 Step -1
If (maskL And oneBit) = oneBit Then
CIDR = CIDR + 1
Else
Exit For
End If
oneBit = oneBit / 2# 'Shift one bit to the right (>> 1)
Next
Dim answer As String
answer = numToIp(ipL And maskL) & " /" & CStr(CIDR)
End Function
Public Function NumHostsInCidr(CIDR As Integer) As Currency
Dim mask As Currency
mask = maskFromCidr(CIDR)
NumHostsInCidr = iPnumOfHosts(mask)
End Function
Private Function maskFromCidr(ByVal CIDR As Integer) As Currency
'x = 32 - CIDR
'z = (2^x)-1
'return z xor 255.255.255.255
maskFromCidr = CLng(2 ^ ((32 - CIDR)) - 1) Xor 4294967295# '255.255.255.255
End Function
Private Function iPnumOfHosts(ByVal IPmsk As Currency) As Currency 'a mask for the host portion
'255.255.255.0 XOR 255.255.255.255 = 255 so 0 to 255 is 256 hosts
iPnumOfHosts = IPmsk Xor 4294967295# '255.255.255.255 , calculate the number of hosts
End Function
Private Function numToIp(ByVal theIP As Currency) As String 'convert number back to IP
Dim IPb(3) As Byte '4 octets
Dim theBit As Integer
theBit = 31 'work MSb to LSb
Dim addr As String 'accumulator for address
Dim x As Integer
For x = 0 To 3 'four octets
Dim y As Integer
For y = 7 To 0 Step -1 '8 bits
If (theIP And CLng(2 ^ theBit)) = CLng(2 ^ theBit) Then 'if the bit is on
IPb(x) = IPb(x) + CByte(2 ^ y) 'accumulate
End If
theBit = theBit - 1
Next
addr = addr & CStr(IPb(x)) & "." 'add current octet to string
Next
numToIp = trimLast(addr, ".")
End Function
Private Function iPToNum(ByVal ip As String) As Currency
Dim IPpart As Variant
Dim IPbyte(3) As Byte
IPpart = Split(ip, ".")
Dim x As Integer
For x = 0 To 3
IPbyte(x) = CByte(IPpart(x))
Next x
iPToNum = (IPbyte(0) * (256 ^ 3)) + (IPbyte(1) * (256 ^ 2)) + (IPbyte(2) * 256#) + IPbyte(3)
End Function
Private Function trimLast(str As String, chr As String)
'****
'* Remove "chr" (if it exists) from end of "str".
'****
trimLast = str
If Right(str, 1) = chr Then trimLast = Left(str, Len(str) - 1)
End Function
Whoah,
it is definitelly interesting functionality. But I would do this in very different way. I would treat IP adress and Mask as array of four bytes. Moreover as far as I remeber (well it was some time ago) CIDR and mask can be converted to each other in very simply way (did you looked at the table?). Why don't you apply bitwise operations to each byte separatelly?
BR.
edit: ok I looked closer at the code. The reason why it is overflowing is that you can't use currency and and. I think and is internally defined as Long and can't return any bigger values. It is very common in other languages too. I remember that once I had this problem in other language (Pascal?). You can try to replace and by division. It will be slow but it can't be matter here I suppose. Other solution is, like I wrote, to treat those valueas all the time as byte arrays and perform bitwise operations on each byte.
This is an entirely mathematical approach to working with IPv4 addresses in VBA (Excel specifically).
The first three functions are serving a strictly supporting role.
Support #1:
Public Function RoundDouble(ByVal Number As Double, ByVal Places As Long) As Double
On Error GoTo Err_RoundDouble
Dim i As Long
Dim j As Long
i = 0
j = 0
While Number < -(2 ^ 14)
Number = Number + (2 ^ 14)
i = i - 1
Wend
While Number > (2 ^ 14)
Number = Number - (2 ^ 14)
i = i + 1
Wend
While Number < -(2 ^ 5)
Number = Number + (2 ^ 5)
j = j - 1
Wend
While Number > (2 ^ 5)
Number = Number - (2 ^ 5)
j = j + 1
Wend
RoundDouble = Round(Number, Places) + (i * (2 ^ 14)) + (j * (2 ^ 5))
Exit_RoundDouble:
Exit Function
Err_RoundDouble:
MsgBox Err.Description
Resume Exit_RoundDouble
End Function
Support #2:
Public Function RoundDownDouble(ByVal Number As Double, ByVal Places As Long) As Double
On Error GoTo Err_RoundDownDouble
Dim i As Double
i = RoundDouble(Number, Places)
If Number < 0 Then
If i < Number Then
RoundDownDouble = i + (10 ^ -Places)
Else
RoundDownDouble = i
End If
Else
If i > Number Then
RoundDownDouble = i - (10 ^ -Places)
Else
RoundDownDouble = i
End If
End If
Exit_RoundDownDouble:
Exit Function
Err_RoundDownDouble:
MsgBox Err.Description
Resume Exit_RoundDownDouble
End Function
Support #3
Public Function ModDouble(ByVal Number As Double, ByVal Divisor As Double) As Double
On Error GoTo Err_ModDouble
Dim rndNumber As Double
Dim rndDivisor As Double
Dim intermediate As Double
rndNumber = RoundDownDouble(Number, 0)
rndDivisor = RoundDownDouble(Divisor, 0)
intermediate = rndNumber / rndDivisor
ModDouble = (intermediate - RoundDownDouble(intermediate, 0)) * rndDivisor
Exit_ModDouble:
Exit Function
Err_ModDouble:
MsgBox Err.Description
Resume Exit_ModDouble
End Function
This first function will convert a Double back into an IP address.
Public Function NUMtoIP(ByVal Number As Double) As String
On Error GoTo Err_NUMtoIP
Dim intIPa As Double
Dim intIPb As Double
Dim intIPc As Double
Dim intIPd As Double
If Number < 0 Then Number = Number * -1
intIPa = RoundDownDouble(ModDouble(Number, (2 ^ 32)) / (2 ^ 24), 0)
intIPb = RoundDownDouble(ModDouble(Number, (2 ^ 24)) / (2 ^ 16), 0)
intIPc = RoundDownDouble(ModDouble(Number, (2 ^ 16)) / (2 ^ 8), 0)
intIPd = ModDouble(Number, (2 ^ 8))
NUMtoIP = intIPa & "." & intIPb & "." & intIPc & "." & intIPd
Exit_NUMtoIP:
Exit Function
Err_NUMtoIP:
MsgBox Err.Description
Resume Exit_NUMtoIP
End Function
This second function is strictly to convert from IPv4 dotted octet format to a Double.
Public Function IPtoNUM(ByVal IP_String As String) As Double
On Error GoTo Err_IPtoNUM
Dim intIPa As Integer
Dim intIPb As Integer
Dim intIPc As Integer
Dim intIPd As Integer
Dim DotLoc1 As Integer
Dim DotLoc2 As Integer
Dim DotLoc3 As Integer
Dim DotLoc4 As Integer
DotLoc1 = InStr(1, IP_String, ".", vbTextCompare)
DotLoc2 = InStr(DotLoc1 + 1, IP_String, ".", vbTextCompare)
DotLoc3 = InStr(DotLoc2 + 1, IP_String, ".", vbTextCompare)
DotLoc4 = InStr(DotLoc3 + 1, IP_String, ".", vbTextCompare)
If DotLoc1 > 1 And DotLoc2 > DotLoc1 + 1 And _
DotLoc3 > DotLoc2 + 1 And DotLoc4 = 0 Then
intIPa = CInt(Mid(IP_String, 1, DotLoc1))
intIPb = CInt(Mid(IP_String, DotLoc1 + 1, DotLoc2 - DotLoc1))
intIPc = CInt(Mid(IP_String, DotLoc2 + 1, DotLoc3 - DotLoc2))
intIPd = CInt(Mid(IP_String, DotLoc3 + 1, 3))
If intIPa <= 255 And intIPa >= 0 And intIPb <= 255 And intIPb >= 0 And _
intIPc <= 255 And intIPc >= 0 And intIPd <= 255 And intIPd >= 0 Then
IPtoNUM = (intIPa * (2 ^ 24)) + (intIPb * (2 ^ 16)) + _
(intIPc * (2 ^ 8)) + intIPd
Else
IPtoNUM = 0
End If
Else
IPtoNUM = 0
End If
Exit_IPtoNUM:
Exit Function
Err_IPtoNUM:
MsgBox Err.Description
Resume Exit_IPtoNUM
End Function
Next we have the conversion from an IPv4 address to it's bitmask representation (assuming that the source entry is a string containing only the dotted octet format of the subnet mask).
Public Function IPtoBitMask(ByVal strIP_Address As String) As Integer
On Error GoTo Err_IPtoBitMask
IPtoBitMask = (32 - Application.WorksheetFunction.Log((2 ^ 32 - IPtoNUM(strIP_Address)), 2))
Exit_IPtoBitMask:
Exit Function
Err_IPtoBitMask:
MsgBox Err.Description
Resume Exit_IPtoBitMask
End Function
This last one is to convert a bitmask back into dotted octet format.
Public Function BitMasktoIP(ByVal intBit_Mask As Integer) As String
On Error GoTo Err_BitMasktoIP
BitMasktoIP = NUMtoIP((2 ^ 32) - (2 ^ (32 - intBit_Mask)))
Exit_BitMasktoIP:
Exit Function
Err_BitMasktoIP:
MsgBox Err.Description
Resume Exit_BitMasktoIP
End Function
Edited to remove leftover debugging code (it's been working for me so long, that I had entirely forgotten about it).
As an aside, it is faster to perform mathematical operations on a computer than it is to work with a string.
This was my "cheating" way:
Option Explicit
Public Function ConvertMaskToCIDR(varMask As Variant) As String
Dim strCIDR As String
Dim mask As String
mask = CStr(varMask)
Select Case mask
Case "255.255.255.255":
strCIDR = "/32"
Case "255.255.255.254":
strCIDR = "/31"
Case "255.255.255.252":
strCIDR = "/30"
Case "255.255.255.248":
strCIDR = "/29"
Case "255.255.255.240":
strCIDR = "/28"
Case "255.255.255.224":
strCIDR = "/27"
Case "255.255.255.192":
strCIDR = "/26"
Case "255.255.255.128":
strCIDR = "/25"
Case "255.255.255.0":
strCIDR = "/24"
Case "255.255.254.0":
strCIDR = "/23"
Case "255.255.252.0":
strCIDR = "/22"
Case "255.255.248.0":
strCIDR = "/21"
Case "255.255.240.0":
strCIDR = "/20"
Case "255.255.224.0":
strCIDR = "/19"
Case "255.255.192.0":
strCIDR = "/18"
Case "255.255.128.0":
strCIDR = "/17"
Case "255.255.0.0":
strCIDR = "/16"
Case "255.254.0.0":
strCIDR = "/15"
Case "255.252.0.0":
strCIDR = "/14"
Case "255.248.0.0":
strCIDR = "/13"
Case "255.240.0.0":
strCIDR = "/12"
Case "255.224.0.0":
strCIDR = "/11"
Case "255.192.0.0":
strCIDR = "/10"
Case "255.128.0.0":
strCIDR = "/9"
Case "255.0.0.0":
strCIDR = "/8"
Case "254.0.0.0":
strCIDR = "/7"
Case "252.0.0.0":
strCIDR = "/6"
Case "248.0.0.0":
strCIDR = "/5"
Case "240.0.0.0":
strCIDR = "/4"
Case "224.0.0.0":
strCIDR = "/3"
Case "192.0.0.0":
strCIDR = "/2"
Case "128.0.0.0":
strCIDR = "/1"
Case "0.0.0.0":
strCIDR = "/0"
End Select
ConvertMaskToCIDR = strCIDR
End Function
Public Function NumUsableIPs(cidr As String) As Long
Dim strHosts As String
If Len(cidr) > 3 Then
'They probably passed a whole address.
Dim slashIndex As String
slashIndex = InStr(cidr, "/")
If slashIndex = 0 Then
NumUsableIPs = 1
Exit Function
End If
cidr = Right(cidr, Len(cidr) - slashIndex + 1)
End If
Select Case cidr
Case "/32":
strHosts = 1
Case "/31":
strHosts = 0
Case "/30":
strHosts = 2
Case "/29":
strHosts = 6
Case "/28":
strHosts = 14
Case "/27":
strHosts = 30
Case "/26":
strHosts = 62
Case "/25":
strHosts = 126
Case "/24":
strHosts = 254
Case "/23":
strHosts = 508
Case "/22":
strHosts = 1016
Case "/21":
strHosts = 2032
Case "/20":
strHosts = 4064
Case "/19":
strHosts = 8128
Case "/18":
strHosts = 16256
Case "/17":
strHosts = 32512
Case "/16":
strHosts = 65024
Case "/15":
strHosts = 130048
Case "/14":
strHosts = 195072
Case "/13":
strHosts = 260096
Case "/12":
strHosts = 325120
Case "/11":
strHosts = 390144
Case "/10":
strHosts = 455168
Case "/9":
strHosts = 520192
Case "/8":
strHosts = 585216
Case "/7":
strHosts = 650240
Case "/6":
strHosts = 715264
Case "/5":
strHosts = 780288
Case "/4":
strHosts = 845312
Case "/3":
strHosts = 910336
Case "/2":
strHosts = 975360
Case "/1":
strHosts = 1040384
End Select
NumUsableIPs = strHosts
End Function