-
VB to VC++
I’m working on a simple chat client to practice network programming w/ VC++ .NET. I have a yahoo messenger login example for the latest protocol (YMSG12) written in VB 6 and I’m converting everything over to VC++. Below are two functions for converting ASCII to HEX and vise versa. These helper functions are used when constructing the login packets. I’m not sure if these two functions can be converted to VC++. For instance “DoEvents” and “UBound”, I have no idea how to convert this, and I could not find anything on google.
Code:
Public Function ChrH(strString) As String
'hex to ascii
Dim A1
A1 = Split(strString, " ")
Dim i As Integer
For i = 0 To UBound(A1)
ChrH = ChrH & Chr("&H" & A1(i))
DoEvents
Next i
End Function
Code:
Public Function AscToHex(strString As String) As String
'ascii to hex
Dim i As Integer
For i = 1 To Len(strString)
If Len(Hex(Asc(Mid(strString, i, 1)))) = 1 Then
AscToHex = AscToHex & "0" & Hex(Asc(Mid(strString, i, 1)))
Else
AscToHex = AscToHex & Hex(Asc(Mid(strString, i, 1)))
End If
If i <> Len(strString) Then AscToHex = AscToHex & " "
DoEvents
Next i
End Function
-
UBound is just the endpoint of the loop.
In C++ you might ask for the size of an STL array.
DoEvents invokes a message loop during what might be a long routine, possibly so it can be cancelled.
You may either ignore it (eliminate it), or obviate it by running this in a thread, if you find that familiar. This will probably run fast enough in C++ that you don't need a DoEvent equivalent.
-
It would run fast enough in VB too. The author was just overly zealous about putting DoEvents everywhere.
-
Thank you, I’m glad to hear this, my past 3 years of programming involve C++ & JAVA I know very little about VB.
It’s a shame, I can find some very good examples online, but, most are written in VB.
-
For what it's worth, here's a proper C++ way of doing the second part.
Code:
std::string char_codes_to_hex_rep(const std::string &s)
{
std::ostringstream out;
out << std::hex << std::setfill('0');
for(std::string iterator i = s.begin(); i != s.end(); ++i) {
out << std::setw(2) << static_cast<int>(*i);
}
return out.str();
}
-
CornedBee, you rock!
I have been debugging this method all day long.
Your’s works like a charm. Thank you so much
-
You're welcome.
As a rule, it doesn't pay to translate from VB to C++.