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