Convert a String to binary sequence in C# with zero padding when not 8 bits of char -


i use method saw in 1 of questions convert ascii binary string:

public string getbits(string input) {     stringbuilder sb = new stringbuilder();     foreach (byte b in asciiencoding.utf8.getbytes(input))     {         sb.append(convert.tostring(b, 2));     }     return sb.tostring(); } 

but, if input message bellow contains chars 'space' or others:

"hello, how you?" 

the above method building sequence of 6 or 7 bit. when try decode long sequence(which know every 8 bit character) big problem part of message chars 6 or 7 or 8 bit , mixed up. how can improve it? thanks

how

public string getbits(string input) {     return string.concat(input.select(x => convert.tostring(x, 2).padleft(8, '0'))); } 

Comments