Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
See more:
Hello codeproject member..
i am tried learning C# course to create an application that could to convert from string to binary and vice versa.

i am successfull to convert from string to binary using this code :

C#
public string StringToBinary(string data)
        {
            StringBuilder sb = new StringBuilder();
            foreach (char c in data.ToCharArray())
            {
                sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
            }
            string result = sb.ToString();
            textBox2.Text = result;
            return sb.ToString();
        }


usage to convert from string to binary
C#
StringToBinary(textBox1.Text);


but my problem is how to convert from binary to string again?
when i am tried using this code i was fail to get the original string. why?

C#
public string BinaryToString(string data)
{
    List<Byte> byteList = new List<Byte>();

    for (int i = 0; i < data.Length; i += 8)
    {
        byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
    }
    string result = byteList.ToString();
    textBox1.Text = result;
    return Encoding.ASCII.GetString(byteList.ToArray());
}


usage
C#
BinaryToString(textBox2.Text);


please teach me, sir. thanks in advance.
Posted

There is no such data type as "binary", so the question is simply ambiguous. As we don't know what is your purpose, you cannot get on definitive answer.

However, you can represent any string in certain encoding used for binary representation of Unicode. The encoding has to be one of the UTFs, as other encodings will cause the loss of data, as the cannot represent all Unicode code points.

For example, for UTF-8 (most popular encoding used to represent Unicode text in text files and on Internet), it would look like this:
C#
string myString = //...

//...

System.Text.Encoding encoding = System.Text.Encoding.UTF8; // change it to any other UTF in you want

byte[] data = encoding.GetBytes(); // got UTF-8 representation

string text = new string(encoding.GetChars(data)); // back to string
// at this point, myString should be the same as text


—SA
 
Share this answer
 
Comments
Gun Gun Febrianza 31-Aug-13 1:44am    
thank you so much sir :)
i am understand what you talking about above.. but i am sorry i am still learning english knowledge.
Sergey Alexandrovich Kryukov 31-Aug-13 2:07am    
You are very welcome.
Add proper capitalization to your English text ('I', start of the clause, 'English', etc.), and your posts would be clear enough.
Good luck, call again.
—SA
Gun Gun Febrianza 31-Aug-13 2:16am    
hehe okay :)
This is your code which i Executed and Got proper output...

private void button1_Click(object sender, EventArgs e)
{
StringToBinary(textBox1.Text);
}

private void button2_Click(object sender, EventArgs e)
{
textBox3.Text = BinaryToString(textBox2.Text);
}

public void StringToBinary(string data)
{
StringBuilder sb = new StringBuilder();
foreach (char c in data.ToCharArray())
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
string result = sb.ToString();
textBox2.Text = result;
//return sb.ToString();
}
public string BinaryToString(string data)
{
List<byte> byteList = new List<byte>();

for (int i = 0; i < data.Length; i += 8)
{
byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}
string result = byteList.ToString();
return Encoding.ASCII.GetString(byteList.ToArray());
}
 
Share this answer
 
Comments
Gun Gun Febrianza 31-Aug-13 1:45am    
thank you so much i will try it ! :)
 
Share this answer
 
Comments
Gun Gun Febrianza 31-Aug-13 1:48am    
problem solved bro thank you so much for participating help me :)
Thanks7872 4-Sep-13 6:00am    
bro..??? :laugh:
Priyanka Bhagwat 4-Sep-13 6:18am    
exactly.......journey from sis to bro... :)
Thanks7872 4-Sep-13 6:20am    
;-)

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