无法从二进制转换为字符串 [英] Failed Convert From Binary To String

查看:83
本文介绍了无法从二进制转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello codeproject成员..

我尝试学习C#课程来创建一个可以从字符串转换为二进制的应用程序,反之亦然。



i使用此代码成功将字符串转换为二进制:



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 :

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

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?

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

BinaryToString(textBox2.Text);





请教我,先生。提前谢谢。



please teach me, sir. thanks in advance.

推荐答案

没有二进制这样的数据类型,所以这个问题很简单。由于我们不知道你的目的是什么,你无法得到确定的答案。



但是,你可以用特定的编码表示用于Unicode二进制表示的任何字符串。编码必须是UTF之一,因为其他编码会导致数据丢失,因为它不能代表所有Unicode代码点。



例如,对于UTF -8(用于在文本文件和Internet上表示Unicode文本的最流行的编码),它看起来像这样:

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:
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


这是我执行并获得正确输出的代码...



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(字符串数据)

{

StringBuilder sb = new StringBuilder();

foreach(char c in data.ToCharArray())

{

sb.Append(Conve rt.ToString(c,2).PadLeft(8,'0'));

}

string result = sb.ToString();

textBox2.Text = result;

//返回sb.ToString();

}

公共字符串BinaryToString(字符串数据)

{

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();

返回Encoding.ASCII.GetString(byteList.ToArray());

}
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());
}


HI ...

希望这个链接可以帮助你..



http://stackoverflow.com/questions/6006425/binary-to-corresponding-ascii-string-conversion [ ^ ]

< br $> b $ b





快乐编码.....
HI...
Hope this link helps You..

http://stackoverflow.com/questions/6006425/binary-to-corresponding-ascii-string-conversion[^]




Happy Coding .....


这篇关于无法从二进制转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆