Search code examples
c#objective-cbitconverter

how to convert short to byte array in Objective-c?


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


Solution

  • 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;
    }