PC Review


Reply
Thread Tools Rate Thread

byte[] to enter key

 
 
Dan Holmes
Guest
Posts: n/a
 
      21st Dec 2006
I am receiving data from the stream of a socket. I am trying to
determine if the user pressed the Enter key. The buffer has 2 non-zero
bytes when the user presses enter: 13 and 10.

If i convert that to ASCII i get \r\n. I would like to have a value i
could compare to ConsoleKeys.Enter if possible.

Anyone know how to do that?

int bytesRead = ns.EndRead(ar);
string txt = Encoding.ASCII.GetString(recvBuffer, 0, bytesRead);


dan
 
Reply With Quote
 
 
 
 
Dave Sexton
Guest
Posts: n/a
 
      21st Dec 2006
Hi Dan,

>I am receiving data from the stream of a socket. I am trying to determine
>if the user pressed the Enter key. The buffer has 2 non-zero bytes when
>the user presses enter: 13 and 10.


What does a socket have to do with the user pressing a key?

> If i convert that to ASCII i get \r\n. I would like to have a value i
> could compare to ConsoleKeys.Enter if possible.


This will get the value of ConsoleKey.Enter:

Console.WriteLine((int) ConsoleKey.Enter);

The result is 13, the value of \r.

Now, what exactly is your question?

<snip>

--
Dave Sexton


 
Reply With Quote
 
Dan Holmes
Guest
Posts: n/a
 
      21st Dec 2006
Dave Sexton wrote:
> Hi Dan,
>
>> I am receiving data from the stream of a socket. I am trying to determine
>> if the user pressed the Enter key. The buffer has 2 non-zero bytes when
>> the user presses enter: 13 and 10.

>
> What does a socket have to do with the user pressing a key?


The user is entering data through a telnet client. The user presses "a"
and i echo an "a" to the terminal.

When the user presses Enter (or Esc or F1) i need to use that as an
indicator that an action needs to be take not just data was entered.

>
>> If i convert that to ASCII i get \r\n. I would like to have a value i
>> could compare to ConsoleKeys.Enter if possible.

>
> This will get the value of ConsoleKey.Enter:
>
> Console.WriteLine((int) ConsoleKey.Enter);
>
> The result is 13, the value of \r.
>
> Now, what exactly is your question?
>
> <snip>
>


The EndRead(...) puts data in a byte array. I would like to know how to
convert that data ({13 ,10} for Enter or {27, 79, 80} for F1 or ...) to
a value in ConsoleKeys without having to write a parser. Certainly this
has been done before.

dan
 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      21st Dec 2006
Hi Dan,

<snip>

>>> If i convert that to ASCII i get \r\n. I would like to have a value i
>>> could compare to ConsoleKeys.Enter if possible.

>>
>> This will get the value of ConsoleKey.Enter:
>>
>> Console.WriteLine((int) ConsoleKey.Enter);
>>
>> The result is 13, the value of \r.
>>
>> Now, what exactly is your question?
>>
>> <snip>
>>

>
> The EndRead(...) puts data in a byte array. I would like to know how to
> convert that data ({13 ,10} for Enter or {27, 79, 80} for F1 or ...) to a
> value in ConsoleKeys without having to write a parser. Certainly this has
> been done before.


I'm still not sure what you mean by "convert the data" or why
ConsoleKey.Enter is required at all. Obviously if {13, 10} indicates a new
line in the stream then ConsoleKey.Enter will not be useful to you since its
value is only 13.

You don't need a parser either. Just read the stream one character at a
time, examining each character along the way. If the current character is
13 then consume the next character immediately and check if it's 10. If so,
process accordingly.

--
Dave Sexton


 
Reply With Quote
 
Mythran
Guest
Posts: n/a
 
      21st Dec 2006


"Dave Sexton" <dave@jwa[remove.this]online.com> wrote in message
news:(E-Mail Removed)...
> Hi Dan,
>
> <snip>
>
>>>> If i convert that to ASCII i get \r\n. I would like to have a value i
>>>> could compare to ConsoleKeys.Enter if possible.
>>>
>>> This will get the value of ConsoleKey.Enter:
>>>
>>> Console.WriteLine((int) ConsoleKey.Enter);
>>>
>>> The result is 13, the value of \r.
>>>
>>> Now, what exactly is your question?
>>>
>>> <snip>
>>>

>>
>> The EndRead(...) puts data in a byte array. I would like to know how to
>> convert that data ({13 ,10} for Enter or {27, 79, 80} for F1 or ...) to a
>> value in ConsoleKeys without having to write a parser. Certainly this
>> has been done before.

>
> I'm still not sure what you mean by "convert the data" or why
> ConsoleKey.Enter is required at all. Obviously if {13, 10} indicates a
> new line in the stream then ConsoleKey.Enter will not be useful to you
> since its value is only 13.
>
> You don't need a parser either. Just read the stream one character at a
> time, examining each character along the way. If the current character is
> 13 then consume the next character immediately and check if it's 10. If
> so, process accordingly.
>
> --
> Dave Sexton
>
>


I remember the good ole days of dial-up BBS's and BBS door games...<sigh> I
miss them. Then the internet boom and the ability to telnet w/o 'dialing
into' the mud...w00t nowadays, it's World of Warcraft, Dark Age of Camelot,
Final Fantasy, and all those MMORPG's....but in any case, they all do what
you are trying to do...parse user input from a remote terminal and execute
chunks of code on the server... (and yeah, there are more than just games
that do this, but hey, I'm a programmer and gamer and still 26 years young!)

Like Dave mentioned, you can read the stream one char at a time and
determine when the user sends \r\n. Remember, as well, the user can send
'\r' + '\n' w/o pressing the enter key (depending on input methods you
provide, the user can send the values via alt-numeric pad key-combinations).
What we did in the old text-based MUD game days was reading a stream of data
from the client until we found the \r. Once found, it would process the
first 'word' of the input as a 'command' and everything else following as a
parameter. So, to make this into a point, you are going to be 'parsing' or
'converting the data' regardless of how you do it...by comparing input
against ConsoleKey.Enter, you are still converting into a format to compare
against an enumeration....

Not sure it helped much, just thought I'd bring back some old memories...

:P

Mythran


 
Reply With Quote
 
Jani Järvinen [MVP]
Guest
Posts: n/a
 
      21st Dec 2006
Hello Dan,

if all you want to do is something really simple, then Dave's and Mythran's
answers should get you started. Remember that not all terminal applications
send both \r and \n, so you might need to live with only a single \r as the
Enter (like Dave pointed out).

PS. This is completely off-topic, but I also miss the old dial-in BBSes. See
this web page, for instance:

http://www.synchro.net/sbbslist.html

And also try "telnet vert.synchro.net". Pretty amusing in 2006. :-)

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(E-Mail Removed)
http://www.saunalahti.fi/janij/


 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      21st Dec 2006
Hi,

"Dave Sexton" <dave@jwa[remove.this]online.com> wrote in message
news:(E-Mail Removed)...
> Hi Dan,
>
>
> I'm still not sure what you mean by "convert the data" or why
> ConsoleKey.Enter is required at all. Obviously if {13, 10} indicates a
> new line in the stream then ConsoleKey.Enter will not be useful to you
> since its value is only 13.


I also think that the OP is not very clear.
What I think he wants is to detect a NewLine sequence from a network stream.

If that is the case you need to compare with Environment.NewLine , PLEASE
NOTE that this only works if both client and server are the same platform.
Otherwise you will need to have a paired list of Platform/NewLine and be
able to detect the client platform somehow.



> You don't need a parser either. Just read the stream one character at a
> time, examining each character along the way. If the current character is
> 13 then consume the next character immediately and check if it's 10. If
> so, process accordingly.


As I mentioned above this will not work always.


--
Ignacio Machin
machin AT laceupsolutions com


 
Reply With Quote
 
Dave Sexton
Guest
Posts: n/a
 
      21st Dec 2006
Hi Ignacio,

That's a good point. If the OP requires support for multiple platforms then
the Environment.NewLine value should be split into an array of characters
and each character read from the stream should be compared to the new line
characters in the same order.

--
Dave Sexton

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.com> wrote in
message news:(E-Mail Removed)...
> Hi,
>
> "Dave Sexton" <dave@jwa[remove.this]online.com> wrote in message
> news:(E-Mail Removed)...
>> Hi Dan,
>>
>>
>> I'm still not sure what you mean by "convert the data" or why
>> ConsoleKey.Enter is required at all. Obviously if {13, 10} indicates a
>> new line in the stream then ConsoleKey.Enter will not be useful to you
>> since its value is only 13.

>
> I also think that the OP is not very clear.
> What I think he wants is to detect a NewLine sequence from a network
> stream.
>
> If that is the case you need to compare with Environment.NewLine , PLEASE
> NOTE that this only works if both client and server are the same platform.
> Otherwise you will need to have a paired list of Platform/NewLine and be
> able to detect the client platform somehow.
>
>
>
>> You don't need a parser either. Just read the stream one character at a
>> time, examining each character along the way. If the current character
>> is 13 then consume the next character immediately and check if it's 10.
>> If so, process accordingly.

>
> As I mentioned above this will not work always.
>
>
> --
> Ignacio Machin
> machin AT laceupsolutions com
>



 
Reply With Quote
 
Markus Mayer
Guest
Posts: n/a
 
      22nd Dec 2006
Dave Sexton, 21.12.2006 23:03:
> Hi Ignacio,
>
> That's a good point. If the OP requires support for multiple platforms then
> the Environment.NewLine value should be split into an array of characters
> and each character read from the stream should be compared to the new line
> characters in the same order.
>


If Dan is working on an TTY emulator, platform awareness is should be
only a matter if he treats the characters as they are - carriage return
and line feed in case of \r\n.
Else, if he's simply checking for \r (by means of platform
non-awareness) then a following \n can be ignored.

So an answer to the question could be:

int bytesRead = ns.EndRead(ar);
string txt = Encoding.ASCII.GetString(recvBuffer, 0, bytesRead);
if( test.IndexOf( (char)ConsoleKeys.Enter ) >= 0 ) {
/// ... full strike
}

Surely the best way would be a check on the buffer whether it contains
(byte)13 or (byte)10.


My two pennies.
Regards,
Mac
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to enter Chinese 4 byte characters (GB18030)in MS Access Murali Choudari Microsoft Access VBA Modules 0 7th Oct 2009 07:32 AM
Not able to enter Chinese 4 byte characters(GB18030)in MSAccess 20 Murali Choudari Microsoft Access Database Table Design 0 6th Oct 2009 05:23 PM
Best Performance File Compare: MD5/SHA1 or Byte-by-Byte Checking? Mahmoud Al-Qudsi Microsoft C# .NET 6 4th Apr 2007 04:48 PM
Converting from byte to string and back to byte ends in different results? mfunkmann@yahoo.com Microsoft C# .NET 1 17th Dec 2006 02:53 PM
Convert native byte array (pointer) to managed byte[] Dave Microsoft Dot NET 1 13th Aug 2003 05:08 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.