use*_*606 2 c# binary winforms
如何从 c#win中的char获取8位二进制数.形成?
就像我写D一样,它应该返回01000100,如果我写T,它应该返回01010100
char c = 'D';
string s = Convert.ToString(c,2); // results in "1000100"
string s2 = s.PadLeft(8, '0'); // results in "01000100"
Run Code Online (Sandbox Code Playgroud)
OP要求更快的解决方案,所以我建议预先计算的查找表
string[] binaries = new string[256];
// Calulate all 8 bit decimal string representations.
// Do this once, on initialization.
for(int i = 0; i < 256; i++)
{
binaries[i] = Convert.ToString(i,2).PadLeft(8, '0');
}
// Get the representation for a character.
char c = 'D';
string s = binaries[c];
Run Code Online (Sandbox Code Playgroud)