0

I'm pretty new to this, so somewhat confused. I want to send "OPTIONS" converted to hex using the Socket class. So here's what I have so far. I have "OPTIONS" converted to hex and want to send "4F5054494F4E53".

    private void button2_Click(object sender, EventArgs e)
    {
        var message = Encoding.ASCII.GetBytes("4F5054494F4E53");
        client.BeginSend(message, 0, message.Length, SocketFlags.None,
                     new AsyncCallback(SendData), client);

    }

But when i set a breakpoint on BeginSend the message byte array contains "52705348" and not "4F5054" etc. How do I deal with this? Thanks

1 Answer 1

2

In ASCII 0x52= char 4, 0x70 = char F etc., to get your expected result just:

var message = Encoding.ASCII.GetBytes("OPTIONS");

To verify:

        byte[] verifyHex = new byte[] { 0x4F, 0x50,  0x54, 0x49, 0x4F, 0x4E, 0x53};
        byte[] verifyDec = new byte[] { 79, 80, 84, 73, 79, 78, 83};

        var message = Encoding.ASCII.GetBytes("OPTIONS");

        if (message.Where((t, i) => t != verifyDec[i] || t != verifyHex[i]).Any())
        {
            MessageBox.Show("Not equal.");
        }
        else
        {
            MessageBox.Show("All three representations are equal.");
        }
Sign up to request clarification or add additional context in comments.

6 Comments

I just tried that and the array contains 79808473797883 but i need it to have the value stated above.
@ChaosPandion I'm not sure how will that help me since I would have to convert it again to a byte array and i'd still have the same problem.
@user1224096: 79 decimal = 0x4F hex = ASCII O, 80 = 0x50 = P, 84 = 0x54 = T, etc. Google ASCII Table
@Roger Stewart I get all that, but I don't understand how I am supposed to convert it correctly. I don't want to hardcode the bytes myself, that would be tedious work. I simply want to send "4F5054494F4E53" which is "OPTIONS" converted to hex.
@user1224096: you stated that you see 79808473797883 when you step through. 79808473797883 is the decimal representation in the debugger. Right-click in the 'Autos' or 'Locals' window when debugging and select 'Hexadecimal display'. This will now show the array contains 4F5054494F4E53.
|

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.