Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to convert text to binary code in Scala

1 Answer

0 votes
object TextToBinary_Scala {
  def text2bin(txt: String): String = {
    val bin = new StringBuilder
    
    for (char <- txt) {
      val binary = Integer.toBinaryString(char.toInt)
      val paddedBinary = binary.reverse.padTo(8, '0').reverse.mkString
      bin.append(paddedBinary).append(" ")
    }
    
    bin.toString()
  }

  def main(args: Array[String]): Unit = {
    println(text2bin("Scala Programming"))
  }
}




/*
run:
  
01010011 01100011 01100001 01101100 01100001 00100000 01010000 01110010 01101111 01100111 01110010 01100001 01101101 01101101 01101001 01101110 01100111 
  
*/

 



answered Nov 18, 2024 by avibootz

Related questions

1 answer 73 views
3 answers 112 views
2 answers 197 views
...