i have 2 nsdata objects, data1 , data2, , i'd bit-wise xor , store result in third nsdata object, xordata.
the first thing tried this:
*data1.bytes^*data2.bytes;
but gave error of:
invalid operands binary expression ('const void' , 'const void')
so tried write function extract bytes of data array of integers, perform xor on integers, save result nsdata object. function looks this:
+(nsdata *) dataxor1:(nsdata *) data1 dataxor2:(nsdata *)data2{ const int *data1bytes = [data1 bytes]; const int *data2bytes = [data2 bytes]; nsmutablearray *xorbytes = [[nsmutablearray alloc] init ]; (int = 0; < data1.length;i++){ [xorbytes addobject:[nsnumber numberwithint:(data1bytes[i]^data2bytes[i])]]; } nsdata *xordata = [nskeyedarchiver archiveddatawithrootobject:xorbytes]; return xordata; }
this runs, gives wrong answers. when test on 2 simple pieces of data (data1 = 0x7038 , data2 = 0x0038), , use nslog output values are, get:
data1bytes[0] = 8070450532247943280 data2bytes[0] = 8070450532247943168 data1bytes[0]^data2bytes[0] = 112 data1bytes[1] = 10376302331560798334 data2bytes[1] = 10376302331560798334 data1bytes[1]^data2bytes[1] = 0
this boggles mind bit because values in dataxbytes arrays totally wrong, they're xor-ing right values! (0x70 ^ 0x00 = 0x70 = 112)
i think might endian-ness problem, when change initialization of data1bytes to:
const int *data1bytes = cfswapint32bigtohost([data1 bytes]);
it runs error when tries access it, saying:
thread 1: exc_bad_access(code=1, address = 0xa08ad539)
is there simpler way xor? if not, how can fix endian problem?
casting int
archiving nsarray
of nsnumber
s not create result you're looking for. you'll want have mutable nsdata
append individual bytes to, like
+(nsdata *) dataxor1:(nsdata *) data1 dataxor2:(nsdata *)data2{ const char *data1bytes = [data1 bytes]; const char *data2bytes = [data2 bytes]; // mutable data individual xor'd bytes added nsmutabledata *xordata = [[nsmutabledata alloc] init]; (int = 0; < data1.length; i++){ const char xorbyte = data1bytes[i] ^ data2bytes[i]; [xordata appendbytes:&xorbyte length:1]; } return xordata; }