Breaking a binary stream

Discussion in 'C' started by luckyvictor, Dec 9, 2009.

  1. luckyvictor

    luckyvictor New Member

    Joined:
    Dec 9, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I am new to programming, I have a task which I have to do the following

    1. I have to read in a text file containing characters as well as space, and I don't know why I can read in the characters only but not space.

    2. after this I convert my data into double type, then convert into binary, and store them into an array with type double/char, and get something like below (2 characters in this case)

    a = [01101101 10110111]

    a can be either in type char or double, and has 2 cells.
    and I want to convert it into

    b= [0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 1]
    so now b is an array with 16 cells, and type is double

    so what i am really doing is to break my message into binary stream

    how should I perform it please?

    Many thanks in advance
     
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    I'm not following you here. A char is 8 bits; a double is 64 bits. They are already stored internally as binary streams.

    What are you trying to manipulate here? What types are a and b above? What do you mean by "cells"?
     
  3. luckyvictor

    luckyvictor New Member

    Joined:
    Dec 9, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    what I am trying to do is to find the binary code of the characters in my message. e.g
    messaeg = I love you
    ASCII is 73 32 108 111 118 101 32 121 111 117
    Binary is 1001001 0100000 1101100 1101111 1110110 1100101 0100000 1111001 1101111 1110101

    and save them in an array which is 1x length of all the bits, so looks like [1 0 0 1 0 0 1 0 1 0 0 0 0 0 1 1 0 1 1 0 0 .......1 1 1 0 1 0 1]
     
  4. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    Break your string into characters and test each bit in the character. If set, put a '1' in a temp string, else put a '0' in the temp string. When the string is built, concatenate to your output string.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
      char *out,tmp[9],buf[]="I Love C++";
      int i,j,x,len=strlen(buf);
    
      out=new char[len*9+1];  //allocate a new buffer for the output
      out[0]=0;  //initialize 
      for(i=0;i<len;i++){
        //test each bit in buf[i]
        for(x=0;x<8;x++){
          tmp[x]=(buf[i]&(1<<(7-x)))?'1':'0';  //set char to '1' if bit is set, else '0'
        }
        tmp[8]=0;  //terminate with a NULL;
        strncat(out,tmp,8);  //Add it to the output
        strncat(out," ",1);  //pad with space between groups
      }
      printf("%s\n",out);
      delete[] out;  //cleanup
      return 0;
    }
    
    Prints:
    Code:
    01001001 00100000 01001100 01101111 01110110 01100101 00100000 01000011 00101011 00101011 
    
     
  5. luckyvictor

    luckyvictor New Member

    Joined:
    Dec 9, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Thanks a lot, I test it and it works, however, I am actually asking this for Matlab, and I think it is similar to C++, but is not in fact.

    So you have any idea what would the code be in matlab? or any forum I can post to ask my question please?
     
  6. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    Sorry. I don't know anything about matlab except that I've heard of it.
     
  7. luckyvictor

    luckyvictor New Member

    Joined:
    Dec 9, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Nevermind, u still helped me a lot...thanks for the code
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice