binary to string code example

Example 1: java int to binary string

public static String makeBinaryString(int n) {
	StringBuilder sb = new StringBuilder();
	while (n > 0) {
    sb.append(n % 2);
		n /= 2;
	}
  	sb.reverse();  	
  	return sb.toString();
}

Example 2: python binary string to int

>>> int('11111111', 2)
255

Example 3: binary to string

let binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

let outputStr = binary.split(' ') //Split string in array of binary chars
   .map(bin => String.fromCharCode(parseInt(bin, 2))) //Map every binary char to real char
   .join(''); //Join the array back to a string

console.log(outputStr);

Example 4: python binary to string

testing = "Satana Blabbers".encode()  # string to bytes
# now bytes to string
print(testing.decode(), testing.decode('utf-8'))  # both are same

Tags:

Java Example