Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[ ENCRYPTION ]converting a char string into a bit string and back

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do I convert a character string, in this case a message into a bit string(s)? Then generate some random bit string to simulate an error. Finally I have to correct the message to get rid of the errors, and come up with the original message. This should simulate an encryption and decryption program. I red something about (triple repetition) that repeats a message three times.If message is x1 x2,
we encode it as x1 x2 x3 x4 x5 x6 where x1=x3=x6 and so on.
A valid encoded message is 00010011. To decode the message use the simple majority rule. To determine x1 we look at x1 x3 x6. If two or three of this bits are 1 then we know that x1=1.For example if we receive 011 111 010 as
011111010 we know that the originl message was 011.
My biggest problem is how can I convert char into bit strings and then convert it back. If you have any suggestions I would be very thankful.

Thank you again Dani D!
 
You could use code like

Code:
#define messageLength 128
char szMessage[messageLength]
for (int i=0;i<messageLength;i++)
{
  char cTemp = szMessage[i];
  for (int j=0;j<8;j++)
  {
    if (cTemp&128)
    {
      //add 1 to bit string
    }
    else
    {
      //add 0 to bit string
    }
    // shift the bits in cTemp one bit left
    cTemp = cTemp << 1;
  }
}
[\code]
You could use very similar code to turn it back to a char string - something like
[code]
char cTemp=0;
for (int i = 0; i<total_bits;i++;
{
  if (bitSting[i]==1)
  {
    cTemp++;
  }
  cTemp = cTemp<<1;
  if ((i+1)%8 == 0)//reached the end of a char
  {
    //add cTemp to char string
    cTemp = 0;
  }
}
[\code]

If you use these code fragments you may have to tinker with them a litle. I havent actually tried them.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top