0

I'm trying to send a hex character to a WebSocket4net socket with a C# client. If I send either of the following syntaxes, it works and sends '12'.

_webSocketClient.Send("\x12");
_webSocketClient.Send("\u0012");

When I try to send this, it sends 'C3 B6'.

_webSocketClient.Send("\xF6");

I don't want to use the overloaded method for sending binary data. I'd like to send this as a hex string. How can I send this character?

I'm setting up the Websocket like this:

 _webSocketClient = new WebSocket4Net.WebSocket("wss://www.example.com", "", null, null, "Mozilla","http://www.example.com", WebSocketVersion.None); 
user2146441
  • 220
  • 1
  • 19
  • 43
  • May be look at https://stackoverflow.com/questions/5794334/how-would-i-send-and-receive-packets-over-a-websocket-in-javascript – BetterLateThanNever Aug 08 '17 at 21:50
  • Thanks for the suggestion. but I don't find the linked question a great help. – user2146441 Aug 08 '17 at 21:53
  • Looks like it's sending the string using UTF-8 encoding and you want ASCII encoding. I'm not familiar with that library so I don't know if there's a simple way to change the encoding that it uses (aside from doing `Encoding.GetBytes` yourself and using the byte array overload, which you said you don't want to do). – Tadmas Aug 08 '17 at 21:54
  • I tried this, but it doesn't work either:_webSocketClient.Send(System.Text.Encoding.UTF8.GetString(new byte[] { 0xF6})); – user2146441 Aug 08 '17 at 21:56
  • No, I meant `byte[] buf = Encoding.ASCII.GetBytes("\xF6");` then `_webSocketClient.Send(buf, 0, buf.Length);`. If you call the overload that takes a string it's going to convert it to UTF-8 no matter how you created the string. – Tadmas Aug 08 '17 at 22:00
  • That sends the byte 3F, not F6.If I change it to byte[] buf = { 0xF6 };, it sends the correct byte. You're right about using Default encoding: your suggestions works if I change it to that. – user2146441 Aug 08 '17 at 22:01
  • Oh, right, because ASCII is 7-bit. That should be `Encoding.Default` for ANSI which I believe is 8-bit. Oops. (Shows how much I work with non-Unicode encodings, I guess.) – Tadmas Aug 08 '17 at 22:03
  • Is there any way to send that byte in any way using a string? I don't want to use the overload that takes a byte array if at all possible. – user2146441 Aug 08 '17 at 22:06

0 Answers0