Click here to Skip to main content
15,888,082 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In Java I have a byte array thus
byte[] request = {
				'e', // 'existence' ?
				'I', 'P', 'A', 'D', 0, // Include IP address
				'N', 'A', 'M', 'E', 0, // Include server name
				'J', 'S', 'O', 'N', 0, // Include server port
            };



But C# doesn't accept the zero terminator - any idea how to create an equivalent C# array ?
Posted
Comments
Sergey Alexandrovich Kryukov 18-Jan-16 12:48pm    
Note that characters are not bytes. Why not using array of char?
And why asking something like that about C#, instead of just learning it? If your knowledge of C# is that insufficient, answers won't help you anyway.
—SA
pkfox 18-Jan-16 12:57pm    
Hi Sergey that's what I've tried and I'm not getting the expected result
Sergey Alexandrovich Kryukov 18-Jan-16 14:38pm    
Then please show what exactly have you tried?
—SA

1 solution

Try:
C#
byte[] request = {
    (byte) 'e', // 'existence' ?
    (byte) 'I', (byte) 'P', (byte) 'A', (byte) 'D', 0, // Include IP address
    (byte) 'N', (byte) 'A', (byte) 'M', (byte) 'E', 0, // Include server name
    (byte) 'J', (byte) 'S', (byte) 'O', (byte) 'N', 0, // Include server port
};
Or more readably:
C#
byte[] request = Encoding.UTF8.GetBytes("eIPAD\0NAME\0JSON\0");
 
Share this answer
 
Comments
CPallini 18-Jan-16 16:17pm    
5.
pkfox 19-Jan-16 4:32am    
With a slight modification OG's solution worked I changed

byte[] request = Encoding.UTF8.GetBytes("eIPAD\0NAME\0JSON\0");
to

byte[] request = Encoding.ASCII.GetBytes("eIPAD\0NAME\0JSON\0");


and it worked ( i.e. I received the expected data )
OriginalGriff 19-Jan-16 4:45am    
I was originally going to suggest ASCII, but Unicode is a bit more common these days! :laugh:
Glad you got it going though. :thumbsup:

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