Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 279
Centurion Avatar asked Dec 19 '25 23:12

Centurion


2 Answers

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);
like image 131
Inder Kumar Rathore Avatar answered Dec 21 '25 16:12

Inder Kumar Rathore


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).

like image 30
brigadir Avatar answered Dec 21 '25 15:12

brigadir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!