I’m getting a NSData * and trying to get it byte by byte but the data is filled with f.
NSData *Data = getData();
cout << "The log way:" << endl;
NSLog(@"%@", Data);
cout << "The data way:" << endl;
char *data = (char *)[Data bytes];
for(int i = 0; i < [Data length]; i++)
{
cout.width(2);
cout.fill(0);
cout << hex << (int)(data[i]) << " ";
}
cout << endl;
What I’m getting as output:
The log way:
/* something long about time and file */<1f9cb0f8>
The data way:
1f ffffff9c ffffffb0 fffffff8
How can I get this data as ints without all those f?
The
chardata type is of undefined signedness, and it seems your compiler (gcc or clang?) decided it should be signed. Therefore, when you cast acharto a larger type, sign extension is used, which fills the extra bits with the same value the most significant bit has. For bytes with a value larger than0x7F, the most significant bit is set, so it breaks the enlargement.What you want is zero extension, which zeroes the extra bits. You can get it by using the
unsigned chartype.This should do it:
By the way,
-[NSData bytes]return aconstpointer. You should honor this and mark your pointer asconsttoo: