RCIX
RCIX

Reputation: 39457

Good way to convert between short and bytes?

I need to take pairs of bytes in, and output shorts, and take shorts in and output pairs of bytes. Here are the functions i've devised for such a purpose:

static short ToShort(short byte1, short byte2)
{
    short number = (short)byte2;
    number <<= 4;
    number += (short)byte1;
    return number;
}
static void FromShort(short number, out byte byte1, out byte byte2)
{
    byte byte2 = (byte)(number >> 4);
    short tempByte = (short)byte2 << 4;
    byte byte1 = (byte)(number - tempByte);
}

I think this is correct but i'm not sure. If this isn't the right way to do it, what is? is there a way to do this already in the framework?

Upvotes: 11

Views: 43612

Answers (6)

Finch Wang
Finch Wang

Reputation: 92

In .net 5/6 or .net standard 2.0+, or you can add System.Memory in Nuget if you are with .NET Fx

var body = new byte[2];
short counter = 2;
MemoryMarshal.Write(body.AsSpan(0,2), ref counter);

Now, short counter has been converted to bytes;

if you wang to convert it back, you just need

short counter = (short)(body[0] + body[1]);

That's all.

Upvotes: -1

TJB
TJB

Reputation: 13497

Use BitConverter

short number = 42;
byte[] numberBytes = BitConverter.GetBytes(number);
short converted = BitConverter.ToInt16(numberBytes);

Upvotes: 40

rcardare
rcardare

Reputation:

System.BitConverter

Upvotes: -1

Marc Gravell
Marc Gravell

Reputation: 1063704

If you want to take bytes... take bytes; and your shifts are off, and | would be more intuitive:

static short ToShort(byte byte1, byte byte2)
{   // using Int32 because that is what all the operations return anyway...
    return (short)((((int)byte1) << 8) | (int)byte2);
}
static void FromShort(short number, out byte byte1, out byte byte2)
{
    byte1 = (byte)(number >> 8); // to treat as same byte 1 from above
    byte2 = (byte)number;
}

Upvotes: 6

Ateş G&#246;ral
Ateş G&#246;ral

Reputation: 140122

Shorter version (also shifting 8 bits instead of 4):

static short ToShort(short byte1, short byte2)
{
    return (byte2 << 8) + byte1;
}

static void FromShort(short number, out byte byte1, out byte byte2)
{
    byte2 = (byte)(number >> 8);
    byte1 = (byte)(number & 255);
}

Upvotes: 26

John Kugelman
John Kugelman

Reputation: 361957

Bytes are 8 bits, not 4, so your shifting is off. You also declared local variables in the second function so you wouldn't end up writing the the out parameters like you intend. It's also clearer/better if you limit yourself to bitwise operations (&, |, and ~) where possible.

static short ToShort(byte byte1, byte byte2)
{
    return (short) ((byte2 << 8) | (byte1 << 0));
}

static void FromShort(short number, out byte byte1, out byte byte2)
{
    byte2 = (byte) (number >> 8);
    byte1 = (byte) (number >> 0);
}

Note that the left and right shifts by zero are unnecessary, strictly speaking. I just put those in for symmetry. Also, personally I'd recommend you just learn bitwise arithmetic cold and skip writing helper functions like these. No need to hide the details with something so fundamental, IMHO.

Upvotes: 5

Related Questions