Centurion
Centurion

Reputation: 14304

How to show content of NSData in bits?

I have NSData and I need to view its content in pure bits. Tried NSLog [NSData description] but it returns NSString. Any suggestions?

Upvotes: 3

Views: 4032

Answers (2)

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

use this for bytes

const char *byte = [data bytes];
NSLog(@"%s",byte);

this is for bits

const char *byte = [data bytes];
unsigned int length = [data length];
for (int i=0; i<length; i++) {
    char n = byte[i];
    char buffer[9];
    buffer[8] = 0; //for null
    int j = 8;
    while(j > 0)
    {
        if(n & 0x01)
        {
            buffer[--j] = '1';
        } else
        {
            buffer[--j] = '0';
        }
        n >>= 1;
    }
    printf("%s ",buffer);

Upvotes: 7

brigadir
brigadir

Reputation: 6942

You can look at these bytes in memory browser window:

void* bytes_memory = [yourData bytes];  // set breakpoint after this line

... after stopping on breakpoint find bytes_memory in Local variables window, right click on it and choose View memory of *bytes_memory.

If you want to print to console bits (in format 10011100), then you will need to convert data into corresponding string representation (here is example).

Upvotes: 3

Related Questions