转换二进制字符串重新presentation字节数组 [英] Convert a binary string representation to a byte array

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

问题描述

您将一个字符串,如01110100011001010111001101110100为一个字节数组,然后使用File.WriteAllBytes使得确切的二​​进制字符串是文件的二进制怎么办。在这种情况下,这将是文本测试。

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.

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

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