Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Breaking a binary stream (http://www.go4expert.com/forums/breaking-binary-stream-t20325/)

luckyvictor 10Dec2009 04:17

Breaking a binary stream
 
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

Gene Poole 10Dec2009 20:10

Re: Breaking a binary stream
 
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"?

luckyvictor 10Dec2009 21:26

Re: Breaking a binary stream
 
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]

Gene Poole 10Dec2009 22:10

Re: Breaking a binary stream
 
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

luckyvictor 10Dec2009 22:20

Re: Breaking a binary stream
 
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?

Gene Poole 10Dec2009 22:30

Re: Breaking a binary stream
 
Sorry. I don't know anything about matlab except that I've heard of it.

luckyvictor 10Dec2009 23:57

Re: Breaking a binary stream
 
Nevermind, u still helped me a lot...thanks for the code


All times are GMT +5.5. The time now is 12:49.