I need to do some int → byte conversion and switch to a big endian for some MIDI data that I write. Right now, I'm doing it like:
int tempo = 500000;
char* a = (char*)&tempo;
inverse(a, 3);
[myMutableData appendBytes:a length:3];
and inverse function:
void inverse(char inver_a[],int j)
{
int i,temp;
j--;
for(i=0;i<(j/2);i++)
{
temp=inver_a[i];
inver_a[i]=inver_a[j];
inver_a[j]=temp;
j--;
}
}
This works, but it is not very clean, and I do not like that I need to specify 3both times (since I have the luxury to know how many bytes it will end).
Is there a more convenient way that I should approach this?
source
share