I need help converting the following JS-function to vb.net please:
function testjs(s) {
var dict = {};
var data = (s + "").split("");
var currChar = data[0];
var oldPhrase = currChar;
var out = [currChar];
var code = 256;
var phrase;
for (var i=1; i<data.length; i++) {
var currCode = data[i].charCodeAt(0);
if (currCode < 256) {
phrase = data[i];
}
else {
phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
}
out.push(phrase);
currChar = phrase.charAt(0);
dict[code] = oldPhrase + currChar;
code++;
oldPhrase = phrase;
}
return out.join("");
}
What my code looks like at the moment:
Private Function questionmarkop(ByVal ph As String, ByVal dictatcurr As String, ByVal ophoc As String) As String
Return If(ph = dictatcurr, dictatcurr, ophoc)
End Function
Private Function testvb(ByVal s As String) As String
Dim dict As New Dictionary(Of Integer, String)
Dim data As Char() = s.ToCharArray
Dim currchar As Char = data(0)
Dim oldphrase As String = currchar
Dim out As String() = {currchar}
Dim code As Integer = 256
Dim phrase As String = ""
Dim ret As String = ""
For i As Integer = 1 To data.Length - 1
Dim currcode As Integer = Convert.ToInt16(data(i))
If currcode < 256 Then
phrase = data(i)
Else
phrase = questionmarkop(phrase, dict(currcode), (oldphrase + currchar))
End If
ReDim Preserve out(out.Length)
out(out.Length - 1) = phrase
currchar = phrase(0)
dict.Item(code) = oldphrase + currchar
code += 1
oldphrase = phrase
Next
For Each str As String In out
ret = ret + str
Next
Return ret
End Function
When inputting the following string s: thisĂaveryshorttestđringtogiĆanexamplefČđackoĆrflow
The function should return: thisisaveryshortteststringtogiveanexampleforstackoverflow
The js function does exactly that, my vb function does not sadly.
The first time (or basically everytime) the if statement is not true, the next character will be wrong. So i figure there is something wrong with the line phrase = questionmarkop(phrase, dict(currcode), (oldphrase + currchar)).
With the test string i provided everything works until this, after that i have the first false char.
Can someone help me figure out what i am doing wrong here?