Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
Please can you help me about code in C# to converting string bit to string char or vice versa,can out to ASCII
for example:
bitstring ="0011111110110101001111110110111100111111110110110011111101101111"
resultstring ="?µ?o?Û?o"
and reversing its...

Can you correct my code if it is the best?

What I have tried:

fist:
string result = "";
            while (value.Length > 0)
            {   var first8 = value.Substring(0, 8);
                value = value.Substring(8);
                var number = Convert.ToInt64(first8, 2);
                result += (char)number;



second

string S = "";
            byte[] asciiBytes = Encoding.ASCII.GetBytes(value);
            for (int i = 0; i < asciiBytes.Length; i++)
                for (int j = 0; j < 8; j++)
                {
                    S += (asciiBytes[i] & 0x80) > 0 ? "1" : "0";
                    asciiBytes[i] <<= 1;
                }
            return S;
Posted
Updated 22-Dec-18 7:36am

It's going to depend on what "endian" format your data is in: Big or Little
But here are two to convert from bits to chars:
private string CharStringFromBitStringLittleEndian(string s)
    {
    StringBuilder sb = new StringBuilder(s.Length / 8 + 1);
    byte temp = 0;
    byte bit = 1;
    foreach (char c in s)
        {
        if (c == '1') temp |= bit;
        bit <<= 1;
        if (bit == 0)
            {
            sb.Append((char)temp);
            bit = 1;
            temp = 0;
            }
        }
    if (bit != 1) sb.Append((char)temp);
    return sb.ToString();
    }
private string CharStringFromBitStringBigEndian(string s)
    {
    StringBuilder sb = new StringBuilder(s.Length / 8 + 1);
    byte temp = 0;
    byte bit = 128;
    foreach (char c in s)
        {
        if (c == '1') temp |= bit;
        bit >>= 1;
        if (bit == 0)
            {
            sb.Append((char)temp);
            bit = 128;
            temp = 0;
            }
        }
    if (bit != 1) sb.Append((char)temp);
    return sb.ToString();
    }
 
Share this answer
 
Solution 1 shows how to convert binary to character.

Now converting a character to binary string representation you can do e.g.

1.) Using .net library
int value = 219;
string binary = Convert.ToString(value, 2).PadLeft(8, '0');

2.) Self made
int value = 219;
StringBuilder sb = new StringBuilder(8);
while(value > 0)
{
	int mod= value % 2;
        if (mod == 0)
        {
        	sb.Insert(0, "0");
        }
	else
        {
        	sb.Insert(0, "1");
        }
        value /= 2;
}
string binary = sb.ToString().PadLeft(8, '0');


Note
Take care, you are working with Unicode for which a character has 16bit while your example assumes using 8 bit character.
 
Share this answer
 
v4
Comments
[no name] 23-Dec-18 5:13am    
In case you like to make it safe for Unicode you simply need to go with 16bit instead of 8bit.

[Edit]
You mentioned ASCII in the subject. ASCII is 7bit. You can read more about this here: ASCII - Wikipedia[^]

... and yes, it is not that easy, to get familar with all this ASCII/ansi/Unicode etc. stuff ;)
Member 12440766 23-Dec-18 17:40pm    
Thank you very much sir .... I have benefited from this information a lot ... Thank you for making me the right way for my project
[no name] 24-Dec-18 7:22am    
You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900