Jump to content

Container Check Digit Calculator


Guest Woody56

Recommended Posts

Guest Woody56

Can anyone make something to work on my verizon SCH-i920 . Here are two possibilities. Thanks in advanced to all as I have no clue . Sorry admin's if I posted incorectly

http://checkdigit.tripod.com/

ContCheckDigit.java -static function that does the algorithm

/* * CheckDigitCalculator.java * * Created on September 3, 2002, 5:18 PM */ import javax.swing.*; /** * * @author alex */ public class CheckDigitCalculator extends javax.swing.JFrame { private JTextField parent = null; /** Creates new form CheckDigitCalculator */ public CheckDigitCalculator() { double width = this.getGraphicsConfiguration().getBounds().getWidth(); double height = this.getGraphicsConfiguration().getBounds().getHeight(); this.setLocation(((int)width-300)/2,((int)height-300)/2); initComponents(); } public CheckDigitCalculator(JTextField parent){ this(); this.parent = parent; String all = parent.getText(); if(all.length()==11){ all = all.substring(0,10); } inputTextField.setText(all); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ private void initComponents() {//GEN-BEGIN:initComponents java.awt.GridBagConstraints gridBagConstraints; jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); inputTextField = new JTextField(new AllCapsLimitDocument(10),null,0); jLabel3 = new javax.swing.JLabel(); goButton = new javax.swing.JButton(); resultLabel = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); getContentPane().setLayout(new java.awt.GridBagLayout()); setTitle("CheckDigit Calculator"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent evt) { exitForm(evt); } }); jLabel1.setText("Container Number CheckDigit"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridwidth = 3; gridBagConstraints.insets = new java.awt.Insets(15, 15, 1, 15); getContentPane().add(jLabel1, gridBagConstraints); jLabel2.setText("Input 10 Characters:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 2; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(15, 10, 15, 10); getContentPane().add(jLabel2, gridBagConstraints); inputTextField.setFont(new java.awt.Font("Courier New", 0, 12)); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 2; gridBagConstraints.ipadx = 100; gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10); getContentPane().add(inputTextField, gridBagConstraints); jLabel3.setText("Result is:"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(15, 10, 15, 10); getContentPane().add(jLabel3, gridBagConstraints); goButton.setText("CRC?"); goButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { goButtonActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 2; gridBagConstraints.gridy = 2; getContentPane().add(goButton, gridBagConstraints); resultLabel.setFont(new java.awt.Font("Courier New", 1, 12)); resultLabel.setText("-----------"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 1; gridBagConstraints.gridy = 3; gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 10); getContentPane().add(resultLabel, gridBagConstraints); jLabel4.setFont(new java.awt.Font("Dialog", 2, 12)); jLabel4.setText("(ISO 6346)"); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.gridy = 1; gridBagConstraints.gridwidth = 3; gridBagConstraints.insets = new java.awt.Insets(0, 0, 15, 0); getContentPane().add(jLabel4, gridBagConstraints); pack(); }//GEN-END:initComponents private void goButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goButtonActionPerformed // Add your handling code here: if(inputTextField.getText()==null){ return; } if(inputTextField.getText().length()!=10){ JOptionPane.showMessageDialog(this,"Input must be 10 characters", "ERROR",JOptionPane.ERROR_MESSAGE); resultLabel.setText("-----------"); return; } int crc = ContCheckDigit.getCRC(inputTextField.getText()); if(crc == -1){ JOptionPane.showMessageDialog(this,"Please use this Format: ABCD123456", "ERROR",JOptionPane.ERROR_MESSAGE); resultLabel.setText("-----------"); return; } if(crc == 10){ JOptionPane.showMessageDialog(this,"It is recomended not to use this number because the CRC is 10 ", "ERROR",JOptionPane.INFORMATION_MESSAGE); crc=0; } resultLabel.setText(inputTextField.getText() + crc); }//GEN-LAST:event_goButtonActionPerformed /** Exit the Application */ private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm if(parent != null){ // if attached to a parent text field parent.setText(resultLabel.getText()); this.hide(); } else{ System.exit(0); // if run as standalone program } }//GEN-LAST:event_exitForm /** * @param args the command line arguments */ public static void main(String args[]) { new CheckDigitCalculator().show(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton goButton; private javax.swing.JTextField inputTextField; private javax.swing.JLabel resultLabel; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables }
Or This http://martfish.word...codes/#comments
[codepublic Dictionary<char, int> AlphabetCodes = new Dictionary<char, int>();02public List<int> PowerOfMultipliers = new List<int>();03 04private void Form1_Load(object sender, EventArgs e)05{06 int step = 10;07 08 //populate dictionary...09 //...create a dictionary entry for all letters of the alphabet using their Ascii value to identify them.10 //if you subtract their ascii value by the value of the first alpha ascii character (in this case 65 for11 //uppercase 'A'), it will give you it's position in the alphabet, Add 10 to this and skip over all multiples12 //of 11 to give you ISO Owner Code numbers for each letter.13 for (int i = 65; i < 91; i++)14 {15 char c = (char)i;16 int pos = i - 65 + step;17 18 if (c == 'A' || c == 'K' || c == 'U') //omit multiples of 11.19 step += 1;20 21 AlphabetCodes.Add(c, pos); //add to dictionary22 }23 24 //populate list...25 //create a list of 10, 2^x numbers for calculation. List should contain 1, 2, 4, 8, 16, 32, 64 etc..26 for (int i = 0; i < 10; i++)27 {28 int result = (int)Math.Pow(2, i); //power of 2 calculation.29 PowerOfMultipliers.Add(result); //add to list.30 }31}32 33private void button1_Click(object sender, EventArgs e)34{35 int total = 0;36 37 if (textBox1.Text.Length == 11) //container numbers must be 11 characters long.38 {39 for (int i = 0; i < 10; i++) //loop through the first 10 characters (the 11th is the check digit!).40 {41 if (AlphabetCodes.ContainsKey(textBox1.Text)) //if the current character is in the dictionary.42 total += (AlphabetCodes[textBox1.Text] * PowerOfMultipliers); //add it's value to the total.43 else44 {45 int serialNumber = (int)textBox1.Text - 48; //it must be a number, so get the number from the char ascii value.46 total += (serialNumber * PowerOfMultipliers); //and add it to the total.47 }48 }49 50 int checkDigit = (int)total % 11; //this should give you the check digit51 52 //The check digit shouldn't equal 10 according to ISO best practice - BUT there are containers out there that do, so we'll53 //double check and set the check digit to 0...again according to ISO best practice.54 if (checkDigit == 10)55 checkDigit = 0;56 57 if (checkDigit != (int)textBox1.Text[10] - 48) //check digit should equal the last character in the textbox.58 MessageBox.Show("Container Number NOT Valid");59 else60 MessageBox.Show("Container Number Valid");61 }62 else63 {64 MessageBox.Show("Container Number must be 11 characters in length");65 }66}]

Link to comment
Share on other sites

  • 2 weeks later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.