Scala之字符【二】

        本系列的全部内容来源于《Scala编程实战》一书。这个系列仅仅将书中对自己工作有用的代码进行了记录,以方便不时之需时进行查询。 

package Strings
/*
Calculate the Adler-32 checksum using Scala
 */
object adl32 {
  val MOD_ADLER :Int = 65521;

  def adler32sum(code:String): Int ={
    var a:Int = 1;
    var b:Int = 0;
    code.getBytes.foreach{
      char =>
        a = (char + a) % MOD_ADLER;
        b = (char + b) % MOD_ADLER;
    }
    b * 65536 + a
    // (b << 16) + a
  }

  def main(args: Array[String]){
     val sum = adler32sum("Wikipedia");
     printf("checksum (int) = %d\n", sum);
     printf("checksum (hex) = %s\n", sum.toHexString);
  }
}

 

你可能感兴趣的:(Scala,Scala,String)