1

I want to create a data packet and write it to socket.
This packet starts with 'C0' hex value.
In php I easily can create it with this code:

$a = "\xC0";  

now I want to create it in c#. How can do this?

2
  • 1
    you'll want to learn about byte[] and string conversions Commented Jun 28, 2012 at 13:01
  • The same I think, have you tried it? Commented Jun 28, 2012 at 13:02

2 Answers 2

7

In C#, strings are Unicode-encoded, so you shouldn't use them for binary data. Instead use a byte array.

To create a byte array starting with C0, do something like this:

byte[] packet = new byte[] { 0xC0, /* Other values */ };

If you have an existing string that you want to send, you can use Encoding.ASCII to convert it to the bytes you need.

Sign up to request clarification or add additional context in comments.

3 Comments

@user1488521: Did this answer your question? If it did, please mark it as the answer, so people know the question has been answered.
Yes it is, but I can't do this because of my low reputation :(
You don't need any reputation to mark it answered. Click the check icon to the left of the answer.
1

If you are sending bytes it's as easy as:

byte a = 0xC0;

If the packet contains text, you can start a string:

string a = "\xC0";

For more information on String Literals: http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.