简体   繁体   English

如何将一串字符转换为二进制字符串并再次返回?

[英]How can i convert a string of characters into binary string and back again?

I need to convert a string into it's binary equivilent and keep it in a string. 我需要将一个字符串转换为它的二进制等效值,并将其保存在字符串中。 Then return it back into it's ASCII equivalent. 然后将其返回到它的ASCII等价物中。

You can encode a string into a byte-wise representation by using an Encoding , eg UTF-8: 您可以使用Encoding (例如UTF-8)将字符串编码为按字节表示:

var str = "Out of cheese error";
var bytes = Encoding.UTF8.GetBytes(str);

To get back a .NET string object: 要获取.NET字符串对象:

var strAgain = Encoding.UTF8.GetString(bytes);
// str == strAgain

You seem to want the representation as a series of '1' and '0' characters; 您似乎希望表示为一系列'1''0'字符; I'm not sure why you do, but that's possible too: 我不确定你为什么这样做,但这也是可能的:

var binStr = string.Join("", bytes.Select(b => Convert.ToString(b, 2)));

Encodings take an abstract string (in the sense that they're an opaque representation of a series of Unicode code points), and map them into a concrete series of bytes. 编码采用抽象字符串(在某种意义上它们是一系列Unicode代码点的不透明表示),并将它们映射到一系列具体的字节。 The bytes are meaningless (again, because they're opaque) without the encoding. 没有编码,字节是没有意义的(再次,因为它们是不透明的)。 But, with the encoding, they can be turned back into a string. 但是,通过编码,它们可以变回字符串。

You seem to be mixing up "ASCII" with strings; 你似乎把“ASCII”和字符串混在一起; ASCII is simply an encoding that deals only with code-points up to 128. If you have a string containing an 'é' , for example, it has no ASCII representation, and so most definitely cannot be represented using a series of ASCII bytes, even though it can exist peacefully in a .NET string object. ASCII只是一种编码 ,只处理高达128的代码点。例如,如果你有一个包含'é'的字符串,它没有ASCII表示,因此绝对不能使用一系列ASCII字节来表示,即使它可以在.NET string对象中安静地存在。

See this article by Joel Spolsky for further reading. 请参阅Joel Spolsky撰写的这篇文章以供进一步阅读。

You can use these functions for converting to binary and restore it back : 您可以使用这些函数转换为二进制文件并将其还原:

public static 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));
    }
    return Encoding.ASCII.GetString(byteList.ToArray());
}

and for converting string to binary : 并将字符串转换为二进制:

public static string StringToBinary(string data)
{
    StringBuilder sb = new StringBuilder();

    foreach (char c in data.ToCharArray())
    {
        sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
    }
    return sb.ToString();
}

Hope Helps You. 希望帮助你。

First convert the string into bytes, as described in my comment and in Cameron's answer; 首先将字符串转换为字节,如我的评论和Cameron的答案中所述; then iterate, convert each byte into an 8-digit binary number (possibly with Convert.ToString , padding appropriately), then concatenate. 然后迭代,将每个字节转换为8位二进制数(可能使用Convert.ToString ,适当填充),然后连接。 For the reverse direction, split by 8 characters, run through Convert.ToInt16 , build up a byte array, then convert back to a string with GetString . 对于反方向,拆分为8个字符,运行Convert.ToInt16 ,构建一个字节数组,然后转换回带有GetString的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM