Welcome
Welcome to <strong>Developer</strong>.

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

CRC32 example

Discuss Java programming technology.

CRC32 example

Postby developer on Sat Feb 16, 2008 11:47 am

CRC32 example

Here is an example how to get CRC32 value:
Code: Select all
import javax.swing.*;
import java.awt.event.*;
import java.util.zip.CRC32;

public class CRC32Test extends JFrame {
   public static void main(String[] args) {
      new CRC32Test();
   }
   
   private JButton buttonOK;
   private JTextField textField;
   
   public CRC32Test() {
      this.setSize(325, 100);
      this.setTitle("CRC32");
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
      ButtonListener b1 = new ButtonListener();
      
      JPanel panel1 = new JPanel();
      
      panel1.add(new JLabel("Enter your text: "));
      
      textField = new JTextField(15);
      panel1.add(textField);
      
      buttonOK = new JButton("OK");
      buttonOK.addActionListener(b1);
      panel1.add(buttonOK);
      
      this.add(panel1);
      this.setVisible(true);
   }
   
   private class ButtonListener implements ActionListener {
      public void actionPerformed(ActionEvent e) {
         if (e.getSource() == buttonOK) {
            String text = textField.getText();
            if (text.length() == 0) {
               JOptionPane.showMessageDialog(CRC32Test.this,
                     "You didn't enter anything!",
                     "Result", JOptionPane.INFORMATION_MESSAGE);
            } else {
               CRC32 crc = new CRC32();
               crc.reset();
               crc.update(text.getBytes());
               JOptionPane.showMessageDialog(CRC32Test.this,
                     "The CRC32 value is " + crc.getValue(),
                     "Result", JOptionPane.INFORMATION_MESSAGE);
            }
            textField.requestFocus();
         }
      }
   }

}

Attachments
CRC32Test.rar
Source code.
(727 Bytes) Not downloaded yet
developer
Site Admin
 
Posts: 37
Joined: Thu Jan 31, 2008 8:26 pm

Return to Java

Who is online

Users browsing this forum: No registered users and 0 guests

cron