-1

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.

Community
  • 1
  • 1
DShah
  • 9,768
  • 11
  • 71
  • 127
  • What problem are you trying to solve? There are already various functions available to do this. Is this just a learning exercise? – Avi Jul 20 '16 at 06:47
  • What is wrong about solution #2? There are also `CFSwapInt32HostToBig()` and `htonl()` which serve the same purpose. – Martin R Jul 20 '16 at 06:52
  • @MartinR With #2 at least since AND is first and variable is signed the sign will be extended in the last one. Should be shift first, AND last. In the original SO answer the variables are unsigned so there it works fine. – Sami Kuhmonen Jul 20 '16 at 06:55
  • @SamiKuhmonen: Actually right-shifting signed integers is *implementation defined* if I remember correctly. But that's why I am asking OP: What issue does (s)he have with the code? – Martin R Jul 20 '16 at 06:59
  • @DShah: See http://stackoverflow.com/questions/20053976/how-can-i-turn-nsdatas-hex-string-into-a-normal-hex-string or http://stackoverflow.com/questions/4931492/appending-data-using-nsmutabledata. – Martin R Jul 20 '16 at 07:07
  • @Avi: I am writing one Crypto function which should work with my server. on server side they have written this above function. So I was just wanted to convert that to objective C function. Not sure exactly why function name is BigEndian. I also understand that Big Endian refers to the big integer values. – DShah Jul 20 '16 at 08:04
  • @MartinR: I am expecting objective C output to match with Java output for this function. I tried above 2 solutions and many more to match the function. Thanks Martin. Your link did helped. Thanks a lot. :) – DShah Jul 20 '16 at 08:59

1 Answers1

2

Your code doesn't convert to Bigendian byte order. It swaps the bytes. So it will convert Bigendian to LittleEndian and vice versa, which means it is completely wrong on a Bigendian processor.

CFSwapInt32HostToBig does exactly what you need: Turn a number into BigEndian format. No matter what your processor is. htnl () does the same thing in a more portable way, except it means "host to network byte order", and network byte order is Bigendian. Then you can add that number to the NSData.

gnasher729
  • 51,477
  • 5
  • 75
  • 98