将二进制字符串表示形式转换为字节数组 [英] Convert a binary string representation to a byte array

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

问题描述

如何将诸如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.

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

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