none
CSharp Signed and Unsigned Byte issue in Socket, MemoryStream and Stream using Visual Studio 2008 SP1 RRS feed

  • Question

  • Dear All,

    I am looking for the concrete solution of Signed Unsigned Integer with Socket, Memory Stream and Stream using Visual Studio 2008 SP1.

    Here is my code snippet for memory stream and socket and wrong results as below.

    // CSharp for Memory Stream
    sbyte[] signed = { -128, -121, 0, 1, 127 };
    byte[] unsigned = (byte[])(Array)signed;
    sbyte[] tmpArr = (sbyte[])(Array)unsigned;
    byte[] retUnsigned = new byte[unsigned.Length];
    Stream memStr = new MemoryStream(unsigned, 0, unsigned.Length);
    memStr.Read(retUnsigned, 0, unsigned.Length);
    
    // result 
    retUnsigned = { 128, 135, 0, 1, 127 };

    // CSharp Socket Client Side
    socket m_sock;
    sbyte[] signed = { -128, -121, 0, 1, 127 };
    m_sock.Send((byte[])(Array)signed, signed.Length, 0);
    
    // CSharp Socket Server Side
    // IAsyncResult ar
    
    nBytesRec = m_sock.EndReceive(ar);
    byte [] byReturn = new byte[nBytesRec];
    Array.Copy( m_byBuff, byReturn, nBytesRec );
    string recData = System.Text.Encoding.ASCII.GetString(byReturn);
    Console.WriteLine("ReceivedData : {0}", recData);
    
    // result is same as memory stream
    byReturn = [128, 135, 0 , 121, 127]

    I hope someone will advise me to get it in a right track.

    Thanks and best regards

    Wednesday, February 27, 2013 1:58 AM

Answers

  • C# does not have a variable type unsigned byte but does allow you to set an integer to a byte, and set a byte to an integer like I did in the code below.  C# for some strange reason does alow you to do the same with an array (byte[] x = int[] y).  So you must do an enumeration using a for loop to convert an int array to a byte array.  Using an integer just means in C# that numbers are being stored as 32 bits instead of 8 bits.

    both Java and C# will store sined numbers in two's compliment form when converting from a byte to an int the MSB bit 8 is just copied to bits 8 to 31 like

           unsigned byte : 0x79 (121)

           signed byte :  0x87 (-121)

           signed integer :  0xFFFFFF87 (-121)

    In C# to convert a negative integer (like -121) to a positive byte you must multiply by -1 like this

           byte a = 0x87;

           if((int)a < 0)

              a = (-1 * (int)a) & 0xFF;   (results 0x79)

           

           static void Main(string[] args)
            {
                byte a = 1;
                int b = a;
                byte c = (byte)(b & 0xFF);
                byte d = 1;
                uint e = d;
                byte f = (byte)(e & 0xFF);
                // CSharp for Memory Stream
                int[] signed = { -128, -121, 0, 1, 127 };
                byte[] unsigned = new byte[signed.Length];
                for (int index = 0; index < signed.Length; index++)
                    unsigned[index] = (byte)(signed[index] & 0xff);
                byte[] retUnsigned = new byte[unsigned.Length];
                Stream memStr = new MemoryStream(unsigned, 0, unsigned.Length);
                memStr.Read(retUnsigned, 0, unsigned.Length);
            }


    jdweng

    Thursday, February 28, 2013 5:08 AM

All replies

  • I'm not sure I understand what you are trying to do.  Look at webpage below

    http://stackoverflow.com/questions/1318933/c-sharp-int-to-byte

    You are starting with a byte (8 bits) and then trying to convert to a sbyte which is 4 bytes (32 bits).  Why are you attempting to send 4 times the amount of data you started with?  I assume this is just test code and you really are tryiing to send/recieve a uint and the webpage shows how to properly do the conversion.


    jdweng

    Wednesday, February 27, 2013 11:15 AM
  • Since unsigned variables cannot hold negative value, the numbers like -128 and -121 cannot be represented correctly using unsigned bytes:

    sbyte b1 = -121;

    byte b2 = (byte)b1; // 135 instead of -121

    This can be explained taking into consideration the binary representation of numbers in computers.

    If you have to deal with negative values, then use signed data types.


    • Edited by Viorel_MVP Wednesday, February 27, 2013 11:39 AM
    Wednesday, February 27, 2013 11:38 AM
  • Hi Joel,

    Since the original data and code used in Java is signed byte for socket and memoery stream.

    So, I also would like to send or save signed byte like -123 via socket and memory stream.

    Pls advise me if there were any way to use for signed byte in C#.

    Best regards

    Thursday, February 28, 2013 1:49 AM
  • Hi Viorel,

    Since the original data and code used in Java is signed byte for socket and memoery stream.

    So, I also would like to send or save signed byte like -123 via socket and memory stream.

    Pls advise me if there were any way to use for signed byte in C#.

    Best regards

    Thursday, February 28, 2013 1:49 AM
  • C# does not have a variable type unsigned byte but does allow you to set an integer to a byte, and set a byte to an integer like I did in the code below.  C# for some strange reason does alow you to do the same with an array (byte[] x = int[] y).  So you must do an enumeration using a for loop to convert an int array to a byte array.  Using an integer just means in C# that numbers are being stored as 32 bits instead of 8 bits.

    both Java and C# will store sined numbers in two's compliment form when converting from a byte to an int the MSB bit 8 is just copied to bits 8 to 31 like

           unsigned byte : 0x79 (121)

           signed byte :  0x87 (-121)

           signed integer :  0xFFFFFF87 (-121)

    In C# to convert a negative integer (like -121) to a positive byte you must multiply by -1 like this

           byte a = 0x87;

           if((int)a < 0)

              a = (-1 * (int)a) & 0xFF;   (results 0x79)

           

           static void Main(string[] args)
            {
                byte a = 1;
                int b = a;
                byte c = (byte)(b & 0xFF);
                byte d = 1;
                uint e = d;
                byte f = (byte)(e & 0xFF);
                // CSharp for Memory Stream
                int[] signed = { -128, -121, 0, 1, 127 };
                byte[] unsigned = new byte[signed.Length];
                for (int index = 0; index < signed.Length; index++)
                    unsigned[index] = (byte)(signed[index] & 0xff);
                byte[] retUnsigned = new byte[unsigned.Length];
                Stream memStr = new MemoryStream(unsigned, 0, unsigned.Length);
                memStr.Read(retUnsigned, 0, unsigned.Length);
            }


    jdweng

    Thursday, February 28, 2013 5:08 AM