我将使用iOS objective-C创建所需输出的字节数组。该方法是从静态浮点数组转换为int8_t数组字节数组。说到实现,我发现float数组中每个float的所有字节都按顺序颠倒过来。输出显示为实际输出。您能否告诉我如何转换每个字节并显示为所需输出?以下是我的工作:
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 preffix[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 *) &preffix, sizeof(preffix));
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;
}
实际输出
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
期望的输出
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
答案 0 :(得分:0)
你正在使用一个可怕的,可怕的,可怕的技巧来交换字节。在代码审查中,您没有任何机会通过代码审查。
更糟糕的是你交换字节0和3 两次,并且相同的字节1和2.猜猜当你交换两次时会发生什么?没有。
您也在假设处理器的字节顺序(因为实际上,您不希望反转字节,您希望将它们置于正确的顺序中)。为此,memcpy从float到uint32_t,然后通过按照你想要的顺序将uint32_t的内容移动24,16,8和0位来提取四个字节。然后处理器的字节顺序无关紧要。
PS。我对你的字节交换代码感到很生气,我甚至没有意识到你没有通过指针......
PPS。大多数人讨厌变量名中的拼写错误,比如“preffix”。