• 0

[C#]Output a character or string as bit patterns


Question

8 answers to this question

Recommended Posts

  • 0

Well just off the top of my head, this would be done by coverting each character to its ordinal value (char.GetNumericValue('a')) and then converting that numeric value into a bitwise pattern... though for the life of me cant recall the school days of converting numbers to bits....

but since there are only 256 bytes for a character, ur bit return string should be limited in length, 11111111 = 255 and 00000001 = 1. So you know the max length of each bit string per character (8 characters long), so you would simply check 8 characters for each char and keeping in mind the zero based array for chars. ie, char(0) is valid but char(256) isnt.

Hope that helps...

if you need more assistance, im sure i can whip up a quick app to demonstrate the methods.

  • 0

Actually I'm working on the so-called chaos encryption, which modifies every bit of a file and in this case because of its high efficiency an image file. I guess I can read the whole file into a string and move beyond, well I'm stuck here. According to your instruction, what exactly it is to check the bits in the character?

  • 0

private static string[] ToBinary(string StringToConvert)
        {
            //Convert to bytes.
            byte[] b = ASCIIEncoding.ASCII.GetBytes(StringToConvert);

            //Create the array to be returned.
            string[] tmp2 = new string[b.Length];

            //Interate through each byte
            for (int i = 0; i < b.Length; i++)
            {
                int x = b[i];
                string tmp = "";
                while (true)
                {
                    if ((x % 2) == 1)
                    {
                        tmp = "1" + tmp;
                    }
                    else
                    {
                        tmp = "0" + tmp;
                    }
                    x /= 2;
                    if (x < 1) break;
                }

                //Make sure the value is 8 chars long.
                tmp2[i] = tmp.PadLeft(8, '0');
            }
            return tmp2;
        }

How about that?

Or:

                private static bool[] ToBinary(char CharToConvert)
        {
            //Convert to bytes.
            byte b = ASCIIEncoding.ASCII.GetBytes(CharToConvert.ToString())[0];

            bool[] tmp2 = new bool[8];

            int x = b;

            for (int i = 7; i >= 0; i--)
            {
                if ((x % 2) == 1)
                {
                    tmp2[i] = true;
                }
                else
                {
                    tmp2[i] = false;
                }
                x /= 2;
                if (x < 1) break;
            }

            return tmp2;
        }

To get it as an bool[8] instead.

Edited by Dayon
  • 0

I think you have the wrong approach.

If this so called encryption operates on bytes, rather than strings you should probably read chunks of the file and operate on the chunk and do whatever you need to do.

I'm not familiar with this encryption (or C#), but you probably need to use bitwise operators such as &, |, ^, etc... (I think in C# it's and, or, xor).

Here's a small C program that prints the binary representation of a 'char'.

#include <stdio.h>

int main() 
{
   char ch;
   int i;
   
   printf("Enter a char: ");
   scanf("%c", &ch);
   
   printf("\nBit representation: ");
   for (i = 0; i < 8; i++) {
      // We AND the char with 1000 0000 or simply 0x80
      if ((ch & 0x80))
         printf("1");
       else
         printf("0");
       
       // Shift ch 1 bit to the left
       ch <<= 1;
   }   
}

Edit: Looks like Dayon beat me to it :)

  • 0

First of all don't use string concatenation, it will be terrible performance-wise (a new string must be created EVERY TIME); if anything use a stringbuilder

Create a memorystream, read in a chunk at a time, that will give you a byte array, which you can manipulate and append to an array of the size of the image that you should also create.

  • 0
  darkmark327 said:
First of all don't use string concatenation, it will be terrible performance-wise (a new string must be created EVERY TIME); if anything use a stringbuilder

Create a memorystream, read in a chunk at a time, that will give you a byte array, which you can manipulate and append to an array of the size of the image that you should also create.

586766221[/snapback]

Yeah, I probably should have made that a little nicer...

  • 0

Or....

        private static string ToBinary(string sourceDataString)
        {
            // Convert String Into Byte Array
            byte[] sourceData = System.Text.ASCIIEncoding.ASCII.GetBytes(sourceDataString);

            // Intialize Memory Space
            StringBuilder binaryString = new StringBuilder(sourceData.Length * 8);

            // Convert Each Byte Into A Binary String
            foreach (byte thisByte in sourceData)
                binaryString.Append(Convert.ToString(thisByte, 2));

            // Return String
            return binaryString.ToString();
        }

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.