按位运算

时间:2011-08-26 15:31:59

标签: objective-c cocoa

- (void)pushDigitalJoin: (NSString*)joinNumber
{       
    char joinByteArray[] = {
        0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27
    };

    int joinIntNumber = ([joinNumber intValue] - 1);
    char *upperByte;
    char *lowerByte;

    NSString *decimalString = [NSString stringWithFormat:@"%i", 0xff];

    *upperByte = joinIntNumber & [decimalString intValue];
    *lowerByte = joinIntNumber >> 8;


     joinByteArray[7]= *upperByte;
     joinByteArray[8] = *lowerByte;

    int i;
    for (i = 0; i < sizeof(joinByteArray); i++) {

        NSLog(@"joinByteArray: position-%i | value-%i",i,joinByteArray[i]);
    }

 }

基本上我有字节数组 我需要根据“joinNumber”更改最后2个字节 然后将这2个字节添加到数组

然而我在joinIntNumber&gt;&gt;上遇到编译错误8和使用和运算符的上述操作似乎不起作用。 (输出总是显示39)

那么我如何正确使用这些按位运算符并将我的2字节添加到数组中呢?

改变评论并显示输出的变化(答案(:

   - (void)pushDigitalJoin: (NSString*)joinNumber
{       
    char joinByteArray[] = {
        0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27, 0x00, 0x00
    };

    int joinIntNumber = ([joinNumber intValue] - 1);
    char upperByte = nil;
    char lowerByte = nil;

   // NSString *decimalString = [NSString stringWithFormat:@"%i", 0xff];

    upperByte = joinIntNumber & 0xff;//[decimalString intValue];
    lowerByte = joinIntNumber >> 8;


    joinByteArray[7]= upperByte;
    joinByteArray[8] = lowerByte;

    int i;
    for (i = 0; i < sizeof(joinByteArray); i++) {

        NSLog(@"joinByteArray: position-%i | value-%x",i,joinByteArray[i]);
    }
}

OUTPUT(joinnumber = 5):

2011-08-26 11:06:07.554 Cameleon[2213:40b] joinByteArray: position-0 | value-5
2011-08-26 11:06:07.555 Cameleon[2213:40b] joinByteArray: position-1 | value-0
2011-08-26 11:06:07.557 Cameleon[2213:40b] joinByteArray: position-2 | value-6
2011-08-26 11:06:07.558 Cameleon[2213:40b] joinByteArray: position-3 | value-0
2011-08-26 11:06:07.559 Cameleon[2213:40b] joinByteArray: position-4 | value-0
2011-08-26 11:06:07.561 Cameleon[2213:40b] joinByteArray: position-5 | value-3
2011-08-26 11:06:07.562 Cameleon[2213:40b] joinByteArray: position-6 | value-27
2011-08-26 11:06:07.563 Cameleon[2213:40b] joinByteArray: position-7 | value-4
2011-08-26 11:06:07.564 Cameleon[2213:40b] joinByteArray: position-8 | value-0

3 个答案:

答案 0 :(得分:4)

  

那么我如何正确使用这些按位运算符并将我的2字节添加到数组中呢?

你没有。该数组在堆栈上声明,并具有固定大小(7个字节)。如果你试图在最后添加值,你将最终踩到堆栈上的其他值并可能破坏堆栈。

不相关,但也有问题:

NSString *decimalString = [NSString stringWithFormat:@"%i", 0xff];

*upperByte = joinIntNumber & [decimalString intValue];

这真的没有任何意义......为什么你只是从int创建一个字符串来接受它的intValue?最好写一下:

*upperByte = joinIntNumber & 0xff;

另一件事......你将upperByte和lowerByte声明为字符指针(char *),但你没有将它们设置为指向任何特定的东西。因此,当您尝试将它们指向的字符设置为上一行时,您最终会将值放入随机位置。

如果你想要一个你可以修改的C数组,你应该声明一个足够大的数组来保存你要添加的任何值,在这种情况下:

char joinByteArray[] = {
    0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27, 0x00, 0x00
};

你也可以使用malloc()和朋友在堆上创建它,但是你需要在一开始就让它足够大以保存额外的值,否则在必要时使用realloc()增长数组添加新值。在任何情况下,都不要写入数组末尾。

答案 1 :(得分:1)

第一个问题:joinByteArray只有7个元素长,所以你不能分配到索引7和8.不确定你在这里尝试做什么。当你收到更多数字时,这个数组应该随着时间的推移而增长吗?如果是这样,最好使用NSMutableArrayNSMutableData并将其存储在实例变量中:

char initialBytes[] = {
    0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27
};
NSMutableData *joinBytes = [[NSMutableData alloc] initWithBytes:initialBytes length:7];

第二个问题:您将upperBytelowerByte声明为指针,但它们应该只是堆栈分配的变量。或者更好的是,使用数组来使以后更容易追加数据:

char newBytes[2];
newBytes[0] = joinIntNumber & 0xff;
newBytes[1] = joinIntNumber >> 8;

完成后,您可以附加到数据:

[joinBytes appendBytes:newBytes length:2];

我不确定你要做的是什么,但你可能想要考虑结束(例如,参见NSSwapHostIntToBig)。

答案 2 :(得分:0)

这可能更接近您所寻找的目标。

- (void)pushDigitalJoin: (NSString*)joinNumber
{       
    //You are appending 2 more values so you need to specify
    //that jointByteArray is 9
    unsigned char joinByteArray[9] = {
        0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27
    };

    int joinIntNumber = ([joinNumber intValue] - 1);

    //Upper and lower byte do not need to be char*
    //unless you want to needlessly malloc memory for them
    char upperByte;
    char lowerByte;

    //0xff is an int (unsigned) so this is useless
    //NSString *decimalString = [NSString stringWithFormat:@"%i", 0xff];

    //To get upper byte you need to know the size of int
    //use int32_t to specify >> 24 so you don't need to use sizeof
    upperByte = joinIntNumber >> ((sizeof(joinIntNumber) - 1) * 8);
    lowerByte = joinIntNumber & 0xFF;

    joinByteArray[7] = upperByte;
    joinByteArray[8] = lowerByte;

    int i;
    for (i = 0; i < sizeof(joinByteArray); i++) {
    //Should log hex since you are manipulating bytes
        NSLog(@"joinByteArray: position-%X | value-%X",i,joinByteArray[i]);
    }
}