如何将诸如01110100011001010111001101110100"之类的字符串转换为字节数组,然后使用 File.WriteAllBytes 使确切的二进制字符串是文件的二进制文件.在这种情况下,它将是文本test".
How do you convert a string such as "01110100011001010111001101110100" to a byte array then used File.WriteAllBytes such that the exact binary string is the binary of the file. In this case it would be the the text "test".
如果你没有这个最近很常见的LINQ迷信,你可以试试正常的方法
In case you don't have this LINQ fetish, so common lately, you can try the normal way
string input ....
int numOfBytes = input.Length / 8;
byte[] bytes = new byte[numOfBytes];
for(int i = 0; i < numOfBytes; ++i)
{
bytes[i] = Convert.ToByte(input.Substring(8 * i, 8), 2);
}
File.WriteAllBytes(fileName, bytes);
LINQ 很棒,但必须有一些限制.
LINQ is great but there must be some limits.
这篇关于将二进制字符串表示形式转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!