-2

I know question is a bit weird, I'm asking out of pure curiosity, as I couldn't find any relevant info around. Also, please feel free to edit title, I know its terrible, but could not make up any better.

Let say I have variable foo of type object, which is either short or ushort. I need to send it over network, so I use BitConverter to transform it into byte[]:

byte[] b = new byte[2];
if(foo is short){
    BitConverter.GetBytes((short)foo, 0);
}else{
    BitConverter.GetBytes((ushort)foo, 0);
}

Network/Socket magic happens, I want my variable back. I know type I am expecting to get, so I call BitConverter.GetUInt16 or GetInt16 properly.

Now, question is - does it actually matter, how I serialized the variable? I mean, bits are the same, so it shouldn't have any meaning, am I correct? So that I could

BitConverter.GetBytes((short)foo, 0);

and then do

BitConverter.GetUInt16(myByteArray, 0);

Anyone?

Pindwin
  • 77
  • 2
  • 7
  • 2
    There are only 65536 shorts. Try them all, and soon you will know the answer. – Eric Lippert Sep 01 '14 at 15:32
  • So would that mean, that for machines with same endianness bytes _will_ be the same? – Pindwin Sep 01 '14 at 15:40
  • @CodeCaster ... what? – Rawling Sep 01 '14 at 15:41
  • @Rawling [`BitConverter.GetBytes()`](http://msdn.microsoft.com/en-us/library/fk3sts66(v=vs.110).aspx): _"The order of bytes in the array returned by the GetBytes method depends on whether the computer architecture is little-endian or big-endian."_ – CodeCaster Sep 01 '14 at 15:43
  • @CodeCaster Sorry, I thought OP was asking about whether the bytes of an `Int16` and a `UInt16` would be the same. – Rawling Sep 01 '14 at 15:44
  • @Rawling I still don't really get what is actually being asked here. If it's _"Is an `UInt16/ushort` the same as a `Int16/short`?"_ , then the answer is _"No"_. – CodeCaster Sep 01 '14 at 15:52
  • For a machine with given endianness, are 16 bits in short/ushort the same? Is there a difference in bits, or only thing different is the way these bits are interpreted? – Pindwin Sep 01 '14 at 15:58
  • Short and ushort are different data types. The bits in them do not mean the same. – CodeCaster Sep 01 '14 at 16:05

2 Answers2

0

If you're transmitting it over the network, you could run into endianness issues (i.e. multibyte values might be stored in different byte order on different architectures). The standard convention when sending a multibyte value over a network is to transform it to Network Byte Order.

The receiver of the multibyte value would then convert it to Host Byte Order.

itsme86
  • 19,266
  • 4
  • 41
  • 57
0

To serialize your variable, you should assign the result of BitConverter.GetBytes() to your byte[]. It doesn't matter if your variable is short or ushort, as those are the same size and hold the same values between 0 and 32767. As long as the size is ok, you should have no problems.

So you may make your code as simple as this:

byte[] b;
if(foo is short || foo is ushort)
    b = BitConverter.GetBytes((short)foo); // You get proper results for ushort as well

However at the decoding site you must know which type you need, for short, you need:

short foo = BitConverter.ToInt16(b, 0);

but if you need an ushort, then you write:

ushort foo = BitConverter.ToUInt16(b, 0);

When you send multibyte variables over the network, you should also ensure that they are in network byte order as @itsme86 mentioned in his answer.

If you need to send both shorts and ushorts, then you also need to send type information to the other end to know if the data type is signed or not. I don't write about it now in detail as it would complicate the code.

szaszm
  • 34
  • 2
  • That's exactly waht I was asking about. Thank you! – Pindwin Sep 01 '14 at 16:21
  • Short and ushort do not have the same range. Try it with a negative value. – CodeCaster Sep 01 '14 at 16:32
  • Yes, they don't, but they have the same size, values between 0 and 32767 are common, and casting from ushort to short doesn't make changes in the representation, so for serialization they can be handled the same way. – szaszm Sep 01 '14 at 16:41