I am trying to translate Morse code into text in VB.net.
Using below code, some of the letters in the text stays untranslated. I believe the problem is with "oddel" which should indicated how many letters should be cut in Mid(morsC, i, oddel), while selecting the case.
Code should be able to translate this >…./ ./ .-../ .-../ ---/< into Hello.
What's weird is that while translating "Hello", first letter "H" is correctly cut as " ..../" but it's not matched with the Case "H"
'Hello = …./ ./ .-../ .-../ ---/
Sub EE15a()
Dim veta As String, morsCod As String, st As String
morsCod = InputBox("Enter the morse code.")
veta = morsCodF(morsCod)
st = "Preklad dle funkce " + Chr(10) + veta + Chr(10)
MsgBox(st)
End Sub
Function morsCodF(morsC As String) As String
Dim pismeno As String, znak As String
Dim i As Integer, j As Integer, oddel As Integer
Dim nasel As Boolean
znak = "/"
morsC = Trim(morsC)
j = 1
For i = 1 To (morsC.Length)
i = j
Do
If Mid(morsC, j, 1) = znak Then
oddel = j - oddel
nasel = True
Exit Do
End If
j = j + 1
Loop Until nasel = True Or j > (morsC.Length)
If i = 1 Then
pismeno = (" " + Mid(morsC, i, oddel))
Else
pismeno = Mid(morsC, i, oddel)
End If
Select Case pismeno '" ...-/"
Case " .-/" : morsCodF = morsCodF + " a"
Case " -.../" : morsCodF = morsCodF + " b"
Case " -.-./" : morsCodF = morsCodF + " c"
Case " -../" : morsCodF = morsCodF + " d"
Case " ./" : morsCodF = morsCodF + " e"
Case " ..-./" : morsCodF = morsCodF + " f"
Case " --./" : morsCodF = morsCodF + " g"
Case " ..../" : morsCodF = morsCodF + " h"
Case " ../" : morsCodF = morsCodF + " i"
Case " .---/" : morsCodF = morsCodF + " j"
Case " -.-/" : morsCodF = morsCodF + " k"
Case " .-../" : morsCodF = morsCodF + " l"
Case " --/" : morsCodF = morsCodF + " m"
Case " -./" : morsCodF = morsCodF + " n"
Case " ---/" : morsCodF = morsCodF + " o"
Case " .--./" : morsCodF = morsCodF + " p"
Case " --.-/" : morsCodF = morsCodF + " q"
Case " .-./" : morsCodF = morsCodF + " r"
Case " .../" : morsCodF = morsCodF + " s"
Case " -/" : morsCodF = morsCodF + " t"
Case " ..-/" : morsCodF = morsCodF + " u"
Case " ...-/" : morsCodF = morsCodF + " v"
Case " .--/" : morsCodF = morsCodF + " w"
Case " -..-/" : morsCodF = morsCodF + " x"
Case " -.--/" : morsCodF = morsCodF + " y"
Case " --../" : morsCodF = morsCodF + " z"
Case "#" : morsCodF = morsCodF + " "
End Select
nasel = False
pismeno = ""
j = j + 1
Next
End Function
Code should translate whole word or sentence, but instead is translating only part of the text. E.g. > …./ ./ .-../ .-../ ---/
expected: "Hello", actual: "e o"
split the string morsCod with "/" as separator
Public Class Form1
Sub EE15a()
Dim veta As String, morsCod As String, st As String
'morsCod = InputBox("Enter the morse code.")
morsCod = "..../ ./ .-../ .-../ ---/ --"
morsCod = " .-/ -.../ -.-./ -../ ./ ..-./ --./ ..../ ../ .---/ -.-/ .-../ --/ -./ ---/ .--./ --.-/ .-./ .../ -/ ..-/ ...-/ .--/ -..-/ -.--/ --.."
veta = morsCodF(morsCod)
st = "Preklad dle funkce " + Chr(10) + veta + Chr(10)
MsgBox(st)
End Sub
Function morsCodF(morsC As String) As String
Dim arr As String() = morsC.Split("/"c)
For Each s As String In arr
Select Case s
Case " .-" : morsCodF = morsCodF + " a"
Case " -..." : morsCodF = morsCodF + " b"
Case " -.-." : morsCodF = morsCodF + " c"
Case " -.." : morsCodF = morsCodF + " d"
Case " ." : morsCodF = morsCodF + " e"
Case " ..-." : morsCodF = morsCodF + " f"
Case " --." : morsCodF = morsCodF + " g"
Case " ...." : morsCodF = morsCodF + " h"
Case " .." : morsCodF = morsCodF + " i"
Case " .---" : morsCodF = morsCodF + " j"
Case " -.-" : morsCodF = morsCodF + " k"
Case " .-.." : morsCodF = morsCodF + " l"
Case " --" : morsCodF = morsCodF + " m"
Case " -." : morsCodF = morsCodF + " n"
Case " ---" : morsCodF = morsCodF + " o"
Case " .--." : morsCodF = morsCodF + " p"
Case " --.-" : morsCodF = morsCodF + " q"
Case " .-." : morsCodF = morsCodF + " r"
Case " ..." : morsCodF = morsCodF + " s"
Case " -" : morsCodF = morsCodF + " t"
Case " ..-" : morsCodF = morsCodF + " u"
Case " ...-" : morsCodF = morsCodF + " v"
Case " .--" : morsCodF = morsCodF + " w"
Case " -..-" : morsCodF = morsCodF + " x"
Case " -.--" : morsCodF = morsCodF + " y"
Case " --.." : morsCodF = morsCodF + " z"
Case "#" : morsCodF = morsCodF + " "
End Select
Next
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
EE15a()
End Sub
End Class
If you need some letters uppercase, you need provide additional logic (for instance, first letter is always uppercase). The following code creates dictionary with codes and their letters, parses Morse code string with Regex, extracting codes one-by-one and fetching letters based on these codes. I have created two versions - with Regex and Split.
Dim morse = New Dictionary(Of String, String) From
{
{".-", "a"},
{"-...", "b"},
{"-.-.", "c"},
{".", "e"},
{"....", "h"},
{".-..", "l"},
{"---", "o"},
{"#", " "}
}
Dim s = "..../ ./ .-../ .-../ ---/"
'// 1. REGEX
'// Search for '.', '-' and '#' in any combinations followed by '/'
Dim mc = Regex.Matches(s, "[\.\-#]+(?=/)")
'// Fetch each code from dictionary and concatenate them
Dim x = String.Join("", mc.Cast(Of Match).Select(Function(m) morse(m.Value)))
'// 2. SPLIT
'// Dim x = String.Join("", s.Split({"/"}, StringSplitOptions.RemoveEmptyEntries).
'// Select(Function(z) morse(z.Replace("/", "").Trim())))
Console.WriteLine(x)
'// Output: hello
I think you will get more feedback of the code if you add default case for the Select Case switch.
Select Case s
Case " .-" : morsCodF = morsCodF + " a"
Case "#" : morsCodF = morsCodF + " "
Case Else : morsCodF = morsCodF + " WRONG! "
End Select
Check does this line oddel = j - oddel works as expected.
Based on the logic you have oddel should be amount of characters after previously found / character. If so, then calculation oddel = j - oddel doesn't look right.
Because you updating i with current j calculation should be oddel = j - (i -1)
Check For loop.
Because i updated every time with current value of j, code will make one extra loop which will produce "wrong" value.
Other suggestions.
Decoding morse code to the text is simple mapping from one string to another.
In vb.net Dictionary(Of String, String) could be good tool for this.
Do you really need / character? Instead you can use simple space as letter delimeter.
Function FromMorse(code As String) As String
Const DELIMETER As Char = " "c
Static Secret As New Dictionary(Of String, String) From
{
{".-", "a"},
{"-...", "b"},
{"-.-.", "c"},
{"-..", "d"},
{".", "e"},
{"..-.", "f"},
{"--.", "g"},
{"....", "h"},
{"..", "i"},
{".---", "j"},
{"-.-", "k"},
{".-..", "l"},
{"--", "m"},
{"-.", "n"},
{"---", "o"},
{".--.", "p"},
{"--.-", "q"},
{".-.", "r"},
{"...", "s"},
{"-", "t"},
{"..-", "u"},
{"...-", "v"},
{".--", "w"},
{"-..-", "x"},
{"-.--", "y"},
{"--..", "z"},
{"#", " "}
}
Return code.Split(DELIMETER).
Select(Function(c) Secret(c)).
Aggregate(New StringBuilder(), Function(b, c) b.Append(c)).
ToString()
End Function
Usage
Dim code = ".... . .-.. .-.. --- # .-- --- .-. .-.. -.."
Dim decoded = FromMorse(code)
Console.WriteLine(decoded) ' => "hello world"
A Dictionary for fast look-up, String.Split for brevity and a StringBuilder to keep from creating new strings on every iteration.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = GetTextFromMorse("..../ ./ .-../ .-../ ---/")
Debug.Print(s)
End Sub
Private DictCode As New Dictionary(Of String, String) From
{
{".-", "a"},
{"-...", "b"},
{"-.-.", "c"},
{"-..", "d"},
{".", "e"},
{"..-.", "f"},
{"--.", "g"},
{"....", "h"},
{"..", "i"},
{".---", "j"},
{"-.-", "k"},
{".-..", "l"},
{"--", "m"},
{"-.", "n"},
{"---", "o"},
{".--.", "p"},
{"--.-", "q"},
{".-.", "r"},
{"...", "s"},
{"-", "t"},
{"..-", "u"},
{"...-", "v"},
{".--", "w"},
{"-..-", "x"},
{"-.--", "y"},
{"--..", "z"},
{"#", " "}
}
Private Function GetTextFromMorse(input As String) As String
Dim strArray() As String = input.Split({"/"}, StringSplitOptions.RemoveEmptyEntries)
Dim sb As New StringBuilder
For Each s In strArray
sb.Append(DictCode(s.Trim))
Next
Return sb.ToString
End Function
Im trying to send json object in Outlook using vba. Here is my code:
Dim Msg As Outlook.MeetingItem
Set Msg = Item
Set recips = Msg.Recipients
Dim regEx As New RegExp
regEx.Pattern = "^\w+\s\w+,\sI351$"
Dim URL As String
URL = "https://webhook.site/55759d1a-7892-4c20-8d15-3b8b7f1bf3b3"
For Each recip In recips
If regEx.Test(recip.AddressEntry) And recip.AddressEntry <> "Application Management Linux1, I351" Then
Dim convertedJson As Object
Set convertedJson = JsonConverter.ParseJson("{""fields"": 123}")
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xhr.Open "POST", URL, False
xhr.setRequestHeader "Content-Type", "application/json"
xhr.Send (convertedJson)
End If
Next
If I send just plane text it works well but i can't send convertedJson. Is it possible to send an object?
UPDATE
I can't even do Debug.Print convertedJson
I was tormented by these libraries in the end I did a very terrible thing
Dim flds, prt, id, smry, descrp, issu, name, lfbrkt, rtbrkt, cma, dbdots, jsTest, issuName As String
flds = "'fields'"
prt = "'project'"
id = "'id'"
smry = "'summary'"
descrp = "'description'"
issu = "'issuetype'"
issuName = "'Improvement'"
name = "'name'"
lfbrkt = "{"
rtbrkt = "}"
cma = ","
dbdots = ":"
jsTest = lfbrkt + flds + dbdots + " " + lfbrkt + vbCrLf + vbTab + prt + dbdots + " " + lfbrkt + vbCrLf + vbTab + vbTab + id + dbdots + " " + "30611" + vbCrLf + vbTab + rtbrkt + cma + vbCrLf + vbTab + smry + dbdots + " " + "'" + CStr(Msg.Subject) + "'" + cma + vbCrLf + vbTab + descrp + dbdots + " " + "'" + CStr(Msg.Body) + "'" + cma + vbCrLf + vbTab + issu + dbdots + " " + lfbrkt + vbCrLf + vbTab + vbTab + name + dbdots + " " + issuName + vbCrLf + vbTab + rtbrkt + vbCrLf + rtbrkt + rtbrkt
And I got this
Dim In_Total As Integer = 0
Dim Ph_Total As Integer = 0
Dim Day_In_Total As Integer = 0
Dim Day_Ph_Total As Integer = 0
Dim Course_Names1(1) As String
Qry = "SELECT DISTINCT Course_Name,Course_ID FROM Courses WHERE (Course_ID IN (SELECT Course_Id FROM Student_Courses_Pref WHERE student_Id IN (SELECT student_Id FROM Student_Info WHERE Inserted_date >= '" + Now.Month.ToString() + "-1-" + Now.Year.ToString() + "' and Inserted_date <= '" + Now.Date + "' and Branch_Code = '" + BName + "') and Branch_Code = '" + BName + "')) and Branch_Code = '" + BName + "' and Active = 'Y' order by Course_Id"
str = str + "<br> <br> <center><font face ='Verdana' size='+1'><B><U> Summary Report of " + MonthName(Now.Month()).ToString() + "," + Now.Year.ToString() + "</B></U> <br><br>"
com.CommandText = Qry
com.Connection = con
dr = com.ExecuteReader
str = str + "<table align='center' style='FONT-FAMILY: Verdana' bgcolor='#336699' width='100%'>"
str = str + "<tr style='FONT-WEIGHT: bold; FONT-SIZE: x-small;FONT-FAMILY: Verdana' bgColor='#b9ceee'><td align='center' width='3%'> Date </td>"
i = 0
While dr.Read
str = str + "<td align='Center' colspan='2' width='30%'>" + dr(0).ToString() + "</td>"
ReDim Preserve Course_Names1(i)
Course_Names1(i) = dr(1)
i = i + 1
End While
dr.Close()
str = str + "<td colspan ='2'>Total</td></tr>"
For i = 0 To Now.Day
If i = 1 Then
str = str + "<tr style='FONT-SIZE: x-small;FONT-FAMILY: Verdana' bgColor='#b9ceee'><td>" + i.ToString() + "<sup>st</sup>" + MonthName(Now.Month()).Substring(0, 3) + "</td>"
ElseIf i = 2 Then
str = str + "<tr style='FONT-SIZE: x-small;FONT-FAMILY: Verdana' bgColor='#b9ceee'><td>" + i.ToString() + "<sup>nd</sup>" + MonthName(Now.Month()).Substring(0, 3) + "</td>"
ElseIf i = 3 Then
str = str + "<tr style='FONT-SIZE: x-small;FONT-FAMILY: Verdana' bgColor='#b9ceee'><td>" + i.ToString() + "<sup>rd</sup>" + MonthName(Now.Month()).Substring(0, 3) + "</td>"
Else
str = str + "<tr style='FONT-SIZE: x-small;FONT-FAMILY: Verdana' bgColor='#b9ceee'><td>" + i.ToString() + "<sup>th</sup>" + MonthName(Now.Month()).Substring(0, 3) + "</td>"
End If
Qry = ""
For j = 0 To Course_Names1.Length - 1 'Here i got the error
Qry = "select count(student_id),enquiry_by from student_info where inserted_date = '" + MonthName(Now.Month).ToString() + " " + i.ToString() + "," + Now.Year.ToString() + "' and student_id in (select student_id from student_courses_pref where branch_code = '" + BName + "' and Course_id = '" + Course_Names1(j).ToString() + "') and branch_code ='" + BName + "' group by Enquiry_By "
com.CommandText = Qry
dr = com.ExecuteReader
While dr.Read()
If dr.HasRows Then
If UCase(dr(1).ToString()) = "I" Then
'str = str + "<td width='15%' bgcolor='#ffffff'>" + dr(0).ToString() + "</td><td width='15%' bgcolor='#c2ffc8'></td>"
In_Total += dr(0)
ElseIf UCase(dr(1).ToString()) = "P" Then
'str = str + "<td width='15%'bgcolor='#ffffff'></td><td width='15%' bgcolor='#c2ffc8'>" + dr(0).ToString() + "</td>"
Ph_Total += dr(0)
End If
Else
'str = str + "<td width='15%' bgcolor='#ffffff'></td><td width='15%' bgcolor='#c2ffc8'></td>"
End If
End While
If In_Total <> 0 Then
str = str + "<td width='15%' bgcolor='#ffffff'>" + In_Total.ToString() + "</td>"
Else
str = str + "<td width='15%' bgcolor='#ffffff'> </td>"
End If
If Ph_Total <> 0 Then
str = str + "<td width='15%' bgcolor='#c2ffc8'>" + Ph_Total.ToString() + "</td>"
Else
str = str + "<td width='15%' bgcolor='#c2ffc8'> </td>"
End If
dr.Close()
Day_In_Total += In_Total
Day_Ph_Total += Ph_Total
In_Total = 0
Ph_Total = 0
Next
str = str + "<td>" + Day_In_Total.ToString() + "</td><td>" + Day_Ph_Total.ToString() + "</td></tr>"
Day_In_Total = 0
Day_Ph_Total = 0
Next
str = str + "</tr></table>"
Where ist str defined?
Is it initialized with Dim str as string=""???
str = str + "<br> <br>...