relot
relot

Reputation: 701

C: print an integer array as binary data

I want to record data from a microphone using alsa. This command:

int buf[4096];
memset(buf, 0, sizeof(buf));
snd_pcm_readi(capture_handle, buf, avail);

writes the microphone data into the integer buffer buf. ( I am not sure if the data that is written by ..._readi is even integer values, the documentation does'nt tell.)

But if I iterate through the buffer the numbers make no sense. As an example I get that buf[60] == -2,600,000,000 so its smaller than the minimum integer if integer is 32 bit. ( as a note this is not my code but I have to work on it). I want to get the binary values of this whole buffer array and make sense of the values and look up in which way they are saved into the buffer so I can recreate the soundwave with this data.

Upvotes: 0

Views: 1471

Answers (3)

You could write your own conversion specifier for printing in binary with printf.

Here's a SO answer on how to do that: https://stackoverflow.com/a/112947

After you code that, you should be able to do:

printf("%b", 5); /* prints 101 */

EDIT:

The good point about this is you can then (if you code it, obviously, which btw is a little tedious) use modifiers:

printf("%'#b", 0x15);

The way I programmed that (I still haven't had the opportunity to test it though), it should print:

0b1'0101

EDIT 2:

That link above explains an old deprecated way of doing it. However, it is very similar to the new one, and from reading it, I could do an implementation using the new method.

I posted my implementation here: https://codereview.stackexchange.com/q/219994/200418

Upvotes: 1

Swordfish
Swordfish

Reputation: 13134

Better use an array of char to record raw data. But to answer your question:

#include <limits.h>
#include <stdio.h>

void print_binary(int value)
{
    unsigned mask = ~(~0u >> 1);  // set highest bit

    // iterate over all bits:
    for (size_t i = 0; i < sizeof(value) * CHAR_BIT; ++i) {
        putchar('0' + !!(value & mask));  // !! converts to bool 0 or 1
        mask >>= 1;  // shift to next lower bit
    }
}

int main(void)
{
    int x = 9;
    print_binary(x);
    putchar('\n');
}

Output:

00000000000000000000000000001001

Shorter:

#include <stdio.h>

void print_binary(int value)
{
    for (unsigned mask = ~(~0u >> 1); mask; mask >>= 1)
        putchar('0' + !!(value & mask));
}


PS: Just to clarify ~(~0u >> 1) (8 bits for simplicity):

~0u                  negate all bits                                    1111 1111
~0u >> 1         shift to right 1 bit and fill up with 0      0111 1111
~(~0u >> 1)    negate that                                         1000 0000

Upvotes: 4

Nitin Jadhav
Nitin Jadhav

Reputation: 527

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>

int main(void) 
{
  int32_t data[5]= {2,1,1,1,1}; /*Example array with small value for easy recognition of binary value */
  int i,array_size,bit_size,bit_print;

  array_size=sizeof(data)/sizeof(data[0]);

  for(i=0;i<array_size;i++)
  {
    for(bit_size=31; bit_size>=0; bit_size--)
    {
        bit_print = data[i] >> bit_size;

        if (bit_print & 1)
          printf("1");
        else
          printf("0");
    }
    printf("\n"); /*this new line is optional if you want to view each value on new line.*/
  }  
}

Upvotes: -1

Related Questions