本文介紹了將二進制字符串表示形式轉換為字節數組的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何將諸如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.
這篇關于將二進制字符串表示形式轉換為字節數組的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!