I was just going through one of the Java code where they were using one of the code for getBigEndian function for a given length. Below is the code that is for JAVA:
// JAVA
public static byte[] getBigEndian32(int length) {
byte[] res = new byte[4];
res[0] = (byte) (length >>> 24);
res[1] = (byte) ((length >>> 16) & 0xFF);
res[2] = (byte) ((length >>> 8) & 0xFF);
res[3] = (byte) (length & 0xFF);
return res;
}
What would be this function looks like in Objective C? I tried below solutions but was not giving me the exact output that above function is returning.
// Tried Solution 1:
NSMutableData *data = [NSMutableData data];
[data appendBytes:(const void *)(length >> 24) length:4];
[data appendBytes:(const void *)(length >> 16 & 0xFF) length:4];
[data appendBytes:(const void *)(length >> 8 & 0xFF) length:4];
[data appendBytes:(const void *)(length & 0xFF) length:4];
Above code was crashing on 2nd line. No helpful logs :(
// Tried Solution 2:
int convert(int num)
{
int b0,b1,b2,b3;
b0= (num & 0x000000FF)>>0;
b1= (num & 0x0000FF00)>>8;
b2= (num & 0x00FF0000)>>16;
b3= (num & 0xFF000000)>>24;
num= (b0<<24) | (b1<<16) | (b2<<8) | (b3<<0) ;
return num;
}
Above solution is from: Convert Little Endian to Big Endian
Also gone through Bit conversion tool in Objective-C. This also didn't help.
Let me have any hint or idea on how to convert this Java code to Objective C code. Thanks in advance.