-1

I am going to create the byte array as desired output using iOS objective-C. The method is converting from static float array to int8_t array bytes array . When it comes to the implementation, I have found that all bytes for each float in the float array are reversed in order. The output is displayed as the Actual Output. Would you please tell me how to convert the bytes each and display as Desired Output ? The below is my working :

 float floatArray[5] = {100.0 , 10.0 , 10.0 , 10.0 , 10.0 };
    NSUInteger lengthN = sizeof(floatArray) ;
    NSLog(@" length %lu" , (unsigned long)lengthN);


    int8_t oneByte = lengthN;
    int8_t prefix[4]  = {0x26, 0x24, 0x61 , oneByte };
     //  NSArray *charArray = arry; //20d = 14h
    //char arry[4]={ 0x26, 0x24, 0x61  , oneByte };
    int8_t data[lengthN + 5];


    memcpy (data, (int8_t *) &prefix, sizeof(prefix));

    memcpy (data+4, (int8_t *) &floatArray,  lengthN );

    int length = (int)lengthN + 5;
    int checkSum = 119 + 97 + (int)lengthN ;

    for(int  i = 4 ; i  < lengthN * 4 ; i *=4 ){
        [self swap: data[ 4*i +3] : data [4*i+ 0]];
        [self swap: data[ 4*i +2] : data [4*i+ 1]];
        [self swap: data[ 4*i +1] : data [4*i+ 2]];
        [self swap: data[ 4*i +0] : data [4*i+ 3]];

    }



- (void)swap:(int8_t)a :(int8_t)b {
    a ^= b;
    b ^= a;
    a ^= b;

}

Actual Output

2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal -56
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 66
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.666 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.666 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.666 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.668 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.668 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.668 marker[2770:1000346] sdjhasdhal 65

Desired output

2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 66
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal -56
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.665 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.666 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.666 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.666 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.667 marker[2770:1000346] sdjhasdhal 65
2014-10-31 18:09:21.668 marker[2770:1000346] sdjhasdhal 32
2014-10-31 18:09:21.668 marker[2770:1000346] sdjhasdhal 0
2014-10-31 18:09:21.668 marker[2770:1000346] sdjhasdhal 0
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jeff Bootsholz
  • 2,971
  • 15
  • 70
  • 141
  • Do you understand about *endianness*? – trojanfoe Oct 31 '14 at 10:19
  • Oh damn , i have only considered memory copying the floats as bytes but I realise it is LSB to be written ! Thank you.] – Jeff Bootsholz Oct 31 '14 at 10:45
  • You need to understand when this needs to be done and when it doesn't. One such example is if the data needs to be transmitted and you don't know the endianness of the other system. In that case functions like `ntohl()` (network to host, long) exist to convert to and from a pre-agreed byte order (big-endian). – trojanfoe Oct 31 '14 at 10:54

1 Answers1

0

You are using a horrible, horrible, horrible trick to swap bytes. In a code review you wouldn't have the slightest chance to get that through.

Worse is that you swap bytes 0 and 3 twice, and the same with bytes 1 and 2. Guess what happens when you swap them twice? Nothing.

You are also making assumptions about the byte ordering of your processor (because in reality, you don't want to reverse the bytes, you want to put them into the right ordering). To do that, memcpy from a float to a uint32_t, then extract four bytes by shifting the contents of that uint32_t by 24, 16, 8 and 0 bits in the order that you want. The byte order of your processor is then irrelevant.

PS. I was so offended by your byte swapping code, I didn't even realise you didn't pass in a pointer...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gnasher729
  • 51,477
  • 5
  • 75
  • 98