Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi my name is vishal for past 3 days i have been breaking my head on how to convert ASCII values of Chr function of vb6 into c# windows forms?
I am using winsock control which i got from COM components from toolbox in c# windows forms with sql server2008.
i was wondering what is equivalent of Chr(250),Chr(251),Chr(104) and Chr(105) of vb6 adodb with Ms access in c# windows forms with sql server2008?
I am using winsock control in c# windows forms in order to establish connection between client and server.
Given below is vb6 code containing Chr values in a timer control:
VB
Private Sub Timer1_Timer(index As Integer)
    On Error GoTo errh
    Timer1(index).Enabled = False
    
    If (Timer1(index).Tag = "") Then   'Sync API call
        sckClient(index).SendData Chr(250) & Chr(0) & Chr(105) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(251)
    Else    'Get Last test data API call
        sckClient(index).SendData Chr(250) & Chr(0) & Chr(104) & Chr("0") & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(0) & Chr(251)
    End If
    Timer1(index).Tag = ""
    Exit Sub
errh:
    MsgBox "syncing error:" & Err.Description, vbCritical
End Sub

where sckClient is name of another winsock control in vb6.
Given below is my translation/interpretation of above code in c# windows forms
C#
private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            if (timer1.Tag == "")
            {
                sckClient.SendData(Convert.ToChar(250) +Convert.ToChar(0) +Convert.ToChar(105) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(251));
            }
            else
            {
                sckClient.SendData(Convert.ToChar(250) +Convert.ToChar(0) +Convert.ToChar(104) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(0) +Convert.ToChar(251));
            }
            timer1.Tag = "";
            return;
        }

where sckClient is name of another winsock control in c# windows forms.

My doudt and question is am i interpreting correctly of Chr(250),Chr(0),Chr(105),Chr(104) and Chr(251) of vb6 adodb with Ms access in c# windows forms with sql server 2008.

I believe that in vb6 that Chr(250),Chr(251),Chr(104),Chr(105) and Chr(0) are referring to ASCII characters.
My question is how to how to bind ASCII equivalent of Chr(250),Chr(251),Chr(104),Chr(105) and Chr(0) of vb6 in c# windows forms?

Am i doing anything wrong in my c# code above? Can anyone help me please? Any help/guidance in solving of this problem would be greatly appreciated.
Posted

You are missing one important thing: .NET char, which is the ToChar() return type is not ASCII. All .NET is based on Unicode; ASCII is only supported as the encoding for streams and using it entails the loss of data in the general case of string data.

One more thing: never use multiple concatenation ('+' operator). It's result is always string, and string is immutable. Do I have to explain the consequence? You copy the growing string back and forth as a whole, very inefficient. Generally, all your lines with SendData are too ad-hoc and hard-coded to be acceptable. Get rid of it. If this is really a constant data, it should be written as a single string literal, without any "Convert".

The final answer to your question depends on what you are going to do with send characters. Yes, when you convert integer to char, you get a Unicode character with the corresponding code point value. If the value happens to be in the ASCII range (which is only 0 to 255), the character will be in the ASCII subset of Unicode. You need to learn how Unicode works — this is not encoding at all, only the UTFs are, and different UTFs gives different binary representation of the same string of code points. Unicode defines mapping between code points, which are understood as mathematical integers abstracted from their computer representations, and characters as cultural entities, abstracted from the detail of their graphical representations. Computer-related representation is defined by UTFs.

If something where you send data expects exactly ASCII, it may not work. So, please see: http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

—SA
 
Share this answer
 
Comments
Member 10248768 12-Jun-14 3:00am    
Dear Mr.Sergey Alexandrovich Kryukov
I have modified the line of sckClient.SendData of timer in c# given below:
sckClient.SendData(((char)250).ToString()+((char)0).ToString()+((char)105).ToString()+((char)0).ToString()+((char)0).ToString()+((char)0).ToString()+((char)0).ToString()+((char)0).ToString()+((char)0).ToString()+((char)251).ToString());
Could this do the trick for me in achieving my required result? Reply please?!
Sergey Alexandrovich Kryukov 16-Jun-14 17:43pm    
it could be:
string stringToSend = "\u00fa\u0068\u0000\u0000\u0000\u0000\u00fb";
...

Figures after '\u' are for hex representation of a character's code point (regardless of encoding; this is just a number, order number of code point, \0020 is a blank space, and so on...).

Using a timer is suspicious. Why not a thread?
—SA
Member 10248768 17-Jun-14 0:42am    
Dear Mr.Sergey Alexandrovich Kryukov
Thank for replying to my query. I know that your solution is correct. I will try your solution today and if every thing comes correct i will post/contribute(the correct answer/c# code) in this forum. So that other users/members will not commit the same mistake as i did and that i have a chance to teach them the correct solution on what should be done regarding this problem.
Dear Mr.Sergey Alexandrovich Kryulov
It's me Vishal. I have found a solution of ASCII codes in C#. Given below is link of where i found it.

http://etutorials.org/Programming/actionscript/Appendix+A.+Unicode+Escape+Sequences+for+Latin+1+Characters/[^]

First sorry for this long link since this is my first time of posting a link in this forum.
I know that you might have taken some pains to understand ASCII codes in c# for giving solution to my problem. I know that you might have seen this link/page of Unicode Escape Sequences for Latin 1 Characters already. I just thought if i post this link/information here it would help other users like me who are trying to find ASCII codes in c#. I hope it helps others. That is the reason why i posted this information here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900