Udp sbyte send

LKHaSSeR 21 Reputation points
2021-02-18T17:52:01.7+00:00

Hello, I am communicating with a device in UDP, I send byte type values ​​from 0 to 255 and some Sbyte type values ​​from -128 to +127. my question arises in relation to the Sbyte type value, How I can send them via UDP while the UDPclient only takes a byte array ? Example when I put -5 in argument 1 it gives the 4 bits on the photo ![69763-capture.jpg][1] [1]: /api/attachments/69763-capture.jpg?platform=QnA

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,288 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points
    2021-02-19T03:20:26.95+00:00

    Just cast your sbyte value to byte and you assign the values to byte array.

    byte[] buffer = new byte[1];
    sbyte a = -5;
    buffer[0] = (byte)a;
    Console.Write(Convert.ToString(buffer[0], 2));   //shows 11111011
    

    Btw, the value you see in the image you upload is correct. You are slicing 4 bytes from the array to write it as Int32, so it becomes 0xFFFFFFFB, where those "FF" are 255 and "FB" is 251.

    0 comments No comments