Objective c 位运算 -(void)pushDigitalJoin:(NSString*)joinNumber { 字符joinByteArray[]={ 0x05、0x00、0x06、0x00、0x00、0x03、0x27 }; int joinNumber=([joinNumber intValue]-1); 字符*大写字节; 半焦*低红细胞; NSString*decimalString=[NSString stringWithFormat:@“%i”,0xff]; *upperByte=joinIntNumber&[decimalString intValue]; *lowerByte=joinIntNumber>>8; joinByteArray[7]=*大写字节; joinByteArray[8]=*低红细胞; int i; 对于(i=0;i>8上遇到编译错误,并且上面使用and运算符的操作似乎不起作用。(输出总是显示39)
基本上我有字节数组 我需要根据“joinNumber”更改最后2个字节 然后将这2个字节添加到数组中 但是,我在joinIntNumber>>8上遇到编译错误,并且上面使用and运算符的操作似乎不起作用。(输出总是显示39) 那么,如何正确使用这些位运算符并将我的2字节添加到数组中呢 为反映评论和显示输出所做的更改(回答(:Objective c 位运算 -(void)pushDigitalJoin:(NSString*)joinNumber { 字符joinByteArray[]={ 0x05、0x00、0x06、0x00、0x00、0x03、0x27 }; int joinNumber=([joinNumber intValue]-1); 字符*大写字节; 半焦*低红细胞; NSString*decimalString=[NSString stringWithFormat:@“%i”,0xff]; *upperByte=joinIntNumber&[decimalString intValue]; *lowerByte=joinIntNumber>>8; joinByteArray[7]=*大写字节; joinByteArray[8]=*低红细胞; int i; 对于(i=0;i>8上遇到编译错误,并且上面使用and运算符的操作似乎不起作用。(输出总是显示39),objective-c,cocoa,Objective C,Cocoa,基本上我有字节数组 我需要根据“joinNumber”更改最后2个字节 然后将这2个字节添加到数组中 但是,我在joinIntNumber>>8上遇到编译错误,并且上面使用and运算符的操作似乎不起作用。(输出总是显示39) 那么,如何正确使用这些位运算符并将我的2字节添加到数组中呢 为反映评论和显示输出所做的更改(回答(: -(void)pushDigitalJoin:(NSString*)joinNumber { 字符joinByteArray[]={ 0x05、0x00、0x
-(void)pushDigitalJoin:(NSString*)joinNumber
{
字符joinByteArray[]={
0x05、0x00、0x06、0x00、0x00、0x03、0x27、0x00、0x00、0x00
};
int joinNumber=([joinNumber intValue]-1);
char upperByte=nil;
char lowerByte=零;
//NSString*decimalString=[NSString stringWithFormat:@“%i”,0xff];
upperByte=joinIntNumber&0xff;//[decimalString intValue];
lowerByte=joinIntNumber>>8;
joinByteArray[7]=大写字节;
joinByteArray[8]=低红细胞;
int i;
对于(i=0;i
输出(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
那么,如何正确使用这些位运算符并将我的2字节添加到数组中呢
不需要。数组是在堆栈上声明的,具有固定大小(7字节)。如果尝试在末尾添加值,则可能会在堆栈上踩踏其他值,并可能损坏堆栈
不相关但也有问题的是:
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
这真的没有任何意义…为什么从int创建字符串只是为了获取它的int值?最好写:
NSString *decimalString = [NSString stringWithFormat:@"%i", 0xff];
*upperByte = joinIntNumber & [decimalString intValue];
还有一件事……您将大写字节和小写字节声明为字符点(char*),但您没有将它们设置为指向任何特定的字符。因此,当您尝试将它们指向的字符设置为上行中的字符时,您最终会将值放入随机位置
如果您想要一个可以修改的C数组,则应声明一个足够大的数组,以容纳要添加的任何值,在本例中:
*upperByte = joinIntNumber & 0xff;
您也可以使用malloc()和friends在堆上创建它,但同样,您需要在一开始就将它设置得足够大,以容纳额外的值,或者在添加新值之前根据需要使用realloc()扩展数组。无论如何,都不要写入超过数组末尾的内容。
那么,如何正确使用这些位运算符并将我的2字节添加到数组中呢
不需要。数组是在堆栈上声明的,具有固定大小(7字节)。如果尝试在末尾添加值,则可能会在堆栈上踩踏其他值,并可能损坏堆栈
不相关但也有问题的是:
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
这真的没有任何意义…为什么从int创建字符串只是为了获取它的int值?最好写:
NSString *decimalString = [NSString stringWithFormat:@"%i", 0xff];
*upperByte = joinIntNumber & [decimalString intValue];
还有一件事……您将大写字节和小写字节声明为字符点(char*),但您没有将它们设置为指向任何特定的字符。因此,当您尝试将它们指向的字符设置为上行中的字符时,您最终会将值放入随机位置
如果您想要一个可以修改的C数组,则应声明一个足够大的数组,以容纳要添加的任何值,在本例中:
*upperByte = joinIntNumber & 0xff;
您也可以使用malloc()和friends在堆上创建它,但同样,您需要在一开始就将它设置得足够大,以容纳额外的值,或者根据需要使用realloc()扩展数组在添加新值之前。在任何情况下,都不要超过数组的末尾进行写入。第一个问题:
joinByteArray
只有7个元素长,因此无法指定索引7和8。不确定您在这里尝试执行的操作。当您收到更多数字时,该数组是否应该随着时间的推移而增长?如果是这样,您最好ff使用NSMutableArray
或NSMutableData
并将其存储在实例变量中:
char joinByteArray[] = {
0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27, 0x00, 0x00
};
第二个问题:您将upperByte
和lowerByte
声明为指针,但它们应该只是堆栈分配的变量。或者更好的方法是,为此使用数组,以便以后更容易追加数据:
char initialBytes[] = {
0x05, 0x00, 0x06, 0x00, 0x00, 0x03, 0x27
};
NSMutableData *joinBytes = [[NSMutableData alloc] initWithBytes:initialBytes length:7];
一旦获得,您可以将以下内容附加到数据:
char newBytes[2];
newBytes[0] = joinIntNumber & 0xff;
newBytes[1] = joinIntNumber >> 8;
我不知道你想做什么,但是你可能想考虑DeaNess(例如,参见),
< P>第一个问题:<代码> CuyBytRay.<代码>只有7个元素长,所以你不能赋值给索引7和8。不确定你在这里做什么。这个数组应该随着你接收MOR而增长吗?- (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]);
}
}