Convert String into Binary Sequence | C # program to convert string into binary string ; utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Utility Reverse function ; Driver code
using System ; public class Node { static void strToBinary ( String s ) { int n = s . Length ; for ( int i = 0 ; i < n ; i ++ ) { int val = s [ i ] ; String bin = " " ; while ( val > 0 ) { if ( val % 2 == 1 ) { bin += '1' ; } else bin += '0' ; val /= 2 ; } bin = reverse ( bin ) ; Console . Write ( bin + " ▁ " ) ; } } static String reverse ( String input ) { char [ ] a = input . ToCharArray ( ) ; int l , r = 0 ; r = a . Length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . Join ( " " , a ) ; } public static void Main ( String [ ] args ) { String s = " geeks " ; strToBinary ( s ) ; } }