CSharp Signed Unsigned Integer with Socket, Memory Stream and Stream RRS feed

  • Question

  • Dear All,

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

    In my point of view, Unsigned Interger(byte) is ok for these classes and how about Signed Integer or sbyte.

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

    Thanks and best regards

    Friday, February 22, 2013 6:41 AM

All replies

  • A socket (like TCP) sends and recieve bytes[], as well as stream classes.  When sending/recieving data you always need to pack/unpack the bytes to varibles like string, int, long, uint, ulong.  the easiest way of understanding this is to assume you have a structure containing all the data you are sending like the code below

        struct SendData
            public int firstInteger;        //4 bytes
            public uint secondInteger;      //4 bytes
            public ulong thirdLong;         //8 bytes
            public float fourthFloat;       //4 bytes
            public double fifthDouble;      //8 bytes

    When sending the above data you will always send 28 bytes.  The send and recieve function must know the order that the data is being sent along with the type of data that is being sent.  The only differences between a uint and int is the most significatant bit.  A uint is only positive and and the highest positive number is twice the highest number that a uint can handle.

    Signed or unsigned can be used and it depends on the usage of which you use.


    Friday, February 22, 2013 1:15 PM
  • Hi Joel,

    Thank you for your advice and I tested in my simple program it worked for (let say 123 and -123) for socket client and server side.

    What do u mean "A uint is only positive and and the highest positive number is twice the highest number that a uint can handle."?

    Ony the thing is how come the data received difference between Java and .NET as below

    data = E.......................7i......           // from Java
    data = E...¨...©.........¡£¥§©¡7i¨..... // from .NET

    data = .Test........................      // from Java
    data = .UTF-8.......................... // from .NET

    Is it because of unicode string or something else reply from server?

    Best regards

    Monday, February 25, 2013 3:30 AM
  • Below as examples of 32 bit variables.  The sign bit (MSB) uses one of the bits of the number and reduces the limits by 2.

    a 32 bit int range is From 0 To 4294967295

    a 32 bit uint range is From : -2147483647 To : 2147483647


    1) int 15 = 0x0000000F

    2)  int 0x8000000F =  -2147483632

    The MSB 8 indicates it is a negative number in two's compliment notation.  Performing 2's compliment you would have -1 * 0x7FFFFFF0 = -2147483632.

    3)  uint 0x8000000F =  2147483663

    It is only a conisitence that the difference in the numbers in 2 and 3 above are only 31.


    It looks like you Java and Net encoding is different.  Microsoft string object has two bytes for every character with a private property to indicate if the character is a one byte or two byte character.  In the net library you use the Encoding method to convert the string object to an byte array.  With encoding you can use the following : Ascii, UTF7, UTF8, or Unicode. 

    UTF8 does no transalation and assumes every character is one byte

    UTF7 removes the MSB byte and assumes every character is one byte

    Ascii removes the MSB byte and removes the non printable chartacter and assumes every character is one byte

    Unicode uses the private property which indicates if the character is one or two bytes.

    I'm not 100% sure but Java treats all characters as one byte and doesn't do any translatations.  Java was originally developed on Unix which treats all data as bytes and a string is just an array of one byte characters.


    • Proposed as answer by Mike Feng Tuesday, February 26, 2013 7:24 AM
    • Unproposed as answer by Mr Ko Ko Wednesday, February 27, 2013 3:18 AM
    Monday, February 25, 2013 10:35 AM
  • Hi Joel,

    Thank you for your more advice and I found out the above error is because of signed and unsigned conditions. Then happened to get other characters and I am concern the sending and receiving negative values of signed bytes using socket.send and socket.receive.

    Pls help me to clear out my doubt with working code snippets.

    Best regards

    Tuesday, February 26, 2013 8:40 AM