Java CRC crc32(String data)

Here you can find the source of crc32(String data)

Description

Generates a CRC 32 Value of String passed

License

Open Source License

Parameter

Parameter Description
data a parameter

Exception

Parameter Description
RuntimeException if UTF-8 is not a supported character set

Return

long crc32 value of input. -1 if string is null

Declaration

public static long crc32(String data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class Main {
    /**/*from  www. ja  v a 2 s  .com*/
     * Generates a CRC 32 Value of String passed
     *
     * @param data
     * @return long crc32 value of input. -1 if string is null
     * @throws RuntimeException if UTF-8 is not a supported character set
     */
    public static long crc32(String data) {
        if (data == null) {
            return -1;
        }

        try {
            // get bytes from string
            byte bytes[] = data.getBytes("UTF-8");
            Checksum checksum = new CRC32();
            // update the current checksum with the specified array of bytes
            checksum.update(bytes, 0, bytes.length);
            // get the current checksum value
            return checksum.getValue();
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. crc(String tempStr)
  2. crc32(File f)
  3. crc32(final InputStream input)
  4. crcFromStream(InputStream stream)