-1

In C# short can be converted to byteArray like this

   public void WriteShort(short value)
    {
        WriteBytes(flip(BitConverter.GetBytes(value)));
    }

I want to implement this in C or Objective-C

Kumar
  • 1,882
  • 2
  • 27
  • 44
yj chan
  • 13
  • 3

2 Answers2

0

This is a good case for the union datatype. Assuming a short is always the same size...

union
{
   short s;
   unsigned char bytes[2];
}u;
wiredog
  • 93
  • 2
  • 7
0

In C (considering short is 2 bytes), you could use this:

char* short_to_byteArr (short value)
{
   static char byte_arr[] = {};
   byte_arr[0] = value & 0x00FF;
   byte_arr[1] = (value>>8) & 0x00FF;
   return byte_arr;
}
Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • You're welcome, a + will be appriciated :P If it solves your problem, you could also mark it as an accepted answer. – Gnqz Jul 21 '15 at 13:13