By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,655 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,655 IT Pros & Developers. It's quick & easy.

checkSum Code

P: 5
Hi, i wrote checksum method to ensure that there are no errors in the header and data in TCP.
The technique calculates the values of the checksum field at the sender side, and stores the result in the checksum field. When the message reaches the destination, the receiver recalculates the checksum. If the final result is all zeroes, this designates that there is no errors in the message. when i run my program the result is not right i don't know where is the problem

the sender and receiver should input message
if the sender and receiver input the same massage the checksum should be zero

thank u :)

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.    import java.util.*;
  4.     public class NW242    {
  5.       static Scanner console=new Scanner (System.in);
  6.        public static void main (String[]args)
  7.       {
  8.          int count=0;
  9.          int carry=0;
  10.          char D=' ';
  11.          char U=' ';
  12.             String sumw="";
  13.             String sumw1="";
  14.          String St="";
  15.          int index=3;
  16.          String ST="";
  17.          String w="";
  18.          String z="";
  19.          int cu=0;
  20.          boolean F=false;
  21.          String AS="";
  22.          String[]arr1=new String[AS.length()];
  23.          String []arr2=new String[arr1.length];
  24.          int [] check=new int[4];
  25.          int wi=0;
  26.          String Str="";
  27.          String S="";
  28.          int co=0;
  29.          int ro=0;
  30.          int CB=0;
  31.          int col=0;
  32.          int i=0;
  33.          int M=0;
  34.          int row=0;
  35.          int Carry=0;
  36.          String [] CH=new String [4];
  37.          int h=3;
  38.          int c=4;
  39.          int g=3;
  40.          int p=0;
  41.          String d="";
  42.          int value;
  43.          boolean Fond=false;
  44.          int sum=0;
  45.       ////////////////////////////////////////////////////////////////////////////////////////////reciv
  46.          int count1=0;
  47.          int carry1=0;
  48.          char D1=' ';
  49.          char U1=' ';
  50.          boolean FO1=false;
  51.          int index1=3;
  52.          String ST1="";
  53.          String w1="";
  54.          String AS1="";
  55.          int [] check1=new int[4];
  56.          String [] CH1=new String [4];
  57.          String Str1="";
  58.          String S1="";
  59.          int co1=0;
  60.          int ro1=0;
  61.          int col1=0;
  62.          int i1=0;
  63.          int cou=0;
  64.          int M1=0;
  65.          int R=0;
  66.  
  67.          int row1=0;
  68.          int Carry1=0;
  69.          int h1=3;
  70.          int c14;
  71.          char z1=' ';
  72.          int g1=3;
  73.          int p1=0;
  74.          String d1="";
  75.          int value1;
  76.          int sum1=0;
  77.  
  78.  
  79.          System.out.println("Enter the Massege");
  80.          String sender=console.nextLine();
  81.          int len=sender.length();
  82.  
  83.          String q="";
  84.          if(len%2==0)
  85.             for (  i = 0; i < sender.length();i ++)
  86.             {
  87.  
  88.                U = sender.charAt(i);
  89.                int j = (int) U;
  90.                String hexstr = Integer.toHexString(j); //hex 
  91.                AS=AS.concat(hexstr);
  92.             }//end for
  93.          else
  94.             q=sender.concat("0");
  95.          for (  i = 0; i < q.length();i ++)
  96.          {
  97.             U = q.charAt(i);
  98.             int j = (int) U;
  99.             String hexstr = Integer.toHexString(j); //hex 
  100.             AS=AS.concat(hexstr);
  101.          }
  102.          System.out.println("ASCII value of "+"" +" is  AS" +"   " + AS + ".");  
  103.          System.out.println(" Full Array "); 
  104.          int r=AS.length()/4;
  105.          String [][] arr=new String [r][c];
  106.          int lenn=arr.length;
  107.  
  108.          for(row = 0 ;row <r;row++)
  109.          {
  110.             for( col=0;col<4;col++)
  111.             {
  112.                char ch=AS.charAt(4*row+col);
  113.                String s = String.valueOf(ch);
  114.                arr[row][col]=s;
  115.                System.out.print(arr[row][col]);
  116.             }
  117.             System.out.println("");
  118.          }
  119.          for(co=3;co>=ro;co--) 
  120.          {// star
  121.             for(ro=0;ro<r;ro++)
  122.             {
  123.  
  124.                ST=arr[ro][co]; 
  125.  
  126.                switch(ST.charAt(0))  
  127.                {
  128.                   case '0':
  129.                      M=0;
  130.                      break; 
  131.                   case '1':
  132.                      M=1;
  133.                      break;
  134.                   case '2':
  135.                      M=2;
  136.                      break;
  137.                   case '3':
  138.                      M=3;
  139.                      break;
  140.                   case '4':
  141.                      M=4;
  142.                      break;
  143.                   case '5':
  144.                      M=5;
  145.                      break;
  146.                   case '6':
  147.                      M=7;
  148.                      break;
  149.                   case '8':
  150.                      M=8;
  151.                      break;
  152.                   case '9':
  153.                      M=9;
  154.                      break;
  155.                   case 'A':
  156.                   case 'a':
  157.                      M=10;
  158.                      break; 
  159.                   case 'B':
  160.                   case 'b':
  161.                      M=11;
  162.                      break;
  163.                   case 'C':
  164.                   case 'c':
  165.                      M=12;
  166.                      break;
  167.                   case 'D':
  168.                   case 'd':
  169.                      M=13;
  170.                      break;
  171.                   case 'E':
  172.                   case 'e':
  173.                      M=14;
  174.                      break;
  175.                   case 'F':
  176.                   case 'f':
  177.                      M=15;
  178.                      break;
  179.                }//end switch
  180.                sum=sum+M;
  181.                sum=sum+carry; 
  182.                carry=0;
  183.                 }
  184.                System.out.println("The sum "+"   "+ (ro) + "=" +sum);
  185.                while(sum>=16)
  186.                {
  187.                   sum=sum-16;
  188.                   Carry++;
  189.                   System.out.println("the carry is " + Carry);
  190.                } 
  191.                switch(sum)
  192.                { //start
  193.                   case 0:CH[index]="0";
  194.                      System.out.println(CH[index]);
  195.  
  196.                      break;
  197.                   case 2:CH[index]="2";
  198.                      System.out.println(CH[index]);
  199.  
  200.                      break;
  201.                   case 3:CH[index]="3";
  202.                      System.out.println(CH[index]);
  203.  
  204.                      break;
  205.                   case 4:CH[index]="4";
  206.                      System.out.println(CH[index]);
  207.  
  208.                      break;
  209.                   case 5:CH[index]="5";
  210.                      System.out.println(CH[index]);
  211.  
  212.                      break;
  213.                   case 6:
  214.                      {CH[index]="6";
  215.                         System.out.println( " CH =" + CH[index]);}
  216.                      break;
  217.                   case 7:CH[index]="7";
  218.                      System.out.println(CH[index]);
  219.  
  220.                      break;
  221.                   case 8:CH[index]="8";
  222.                      System.out.println(CH[index]);
  223.  
  224.                      break;
  225.                   case 9:CH[index]="9";System.out.println(CH[index]);
  226.  
  227.                      break;
  228.                   case 10:CH[index]="A";
  229.                      System.out.println(CH[index]);
  230.  
  231.                      break;
  232.                   case 11:CH[index]="B";
  233.                      System.out.println(CH[index]);
  234.  
  235.                      break;
  236.                   case 12:CH[index]="C"; 
  237.                      System.out.println(CH[index]);
  238.  
  239.                      break;
  240.                   case 13:CH[index]="D";
  241.                      System.out.println(CH[index]);
  242.  
  243.                      break;
  244.                   case 14:CH[index]="E";
  245.                      System.out.println(CH[index]);
  246.  
  247.  
  248.                      break;
  249.                   case 15:CH[index]="F";
  250.                      System.out.println(CH[index]);
  251.                      break;
  252.                }
  253.                index--;
  254.                System.out.println(" Sum is " + sum);
  255.                     sumw=Integer.toString(sum);
  256.  
  257.                sum=0;
  258.                ro=0;
  259.             }
  260.          // smallest for
  261.             while(Carry!=0)
  262.             {
  263.                for( h=3;h>=0;h--)
  264.                {
  265.                   CH[h]=sumw;
  266.  
  267.                   switch(CH[h].charAt(0))
  268.                   {
  269.                   // from A 2 F and from 0 2  9 
  270.                      case 'A':
  271.                      case 'a':
  272.                         sum=10+Carry;
  273.                         Carry=0;
  274.                         while (sum>=16)
  275.                         {
  276.                            sum=sum-16;
  277.                            Carry++;
  278.                         }
  279.                         check[h]=sum;
  280.                         break;
  281.                      case 'B':
  282.                      case 'b':
  283.                         sum=11+Carry;
  284.                         Carry=0;
  285.                         while (sum>=16)
  286.                         {
  287.                            sum=sum-16;
  288.                            Carry++;
  289.                         }
  290.                         check[h]=sum;
  291.                         break;
  292.                      case 'C':
  293.                      case 'c':
  294.                         sum=12+Carry;
  295.                         Carry=0;
  296.                         while (sum>=16)
  297.                         {
  298.                            sum=sum-16;
  299.                            Carry++;
  300.                         }
  301.                         check[h]=sum;
  302.                         break;
  303.                      case 'D':
  304.                      case 'd':
  305.                         sum=13+Carry;
  306.                         Carry=0;
  307.                         while (sum>=16)
  308.                         {
  309.                            sum=sum-16;
  310.                            Carry++;
  311.                         }
  312.                         check[h]=sum;
  313.                         break;
  314.                      case 'E':
  315.                      case 'e':
  316.                         sum=14+Carry;
  317.                         Carry=0;
  318.                         while (sum>=16)
  319.                         {
  320.                            sum=sum-16;
  321.                            Carry++;
  322.                         }
  323.                         check[h]=sum;
  324.                         break;
  325.                      case 'F':
  326.                      case 'f':
  327.                         sum=15+Carry;
  328.                         Carry=0;
  329.                         while (sum>=16)
  330.                         {
  331.                            sum=sum-16;
  332.                            Carry++;
  333.                         }
  334.                         check[h]=sum;
  335.                         break;
  336.                      case '0':
  337.                         sum=0+Carry;
  338.                         Carry=0;
  339.                         while (sum>=16)
  340.                         {
  341.                            sum=sum-16;
  342.                            Carry++;
  343.                         }
  344.                         check[h]=sum;
  345.                         break;
  346.                      case '1':
  347.                         sum=1+Carry;
  348.                         Carry=0;
  349.                         while (sum>=16)
  350.                         {
  351.                            sum=sum-16;
  352.                            Carry++;
  353.                         }
  354.                         check[h]=sum;
  355.                         break;
  356.  
  357.                      case '2':
  358.                         sum=2+Carry;
  359.                         Carry=0;
  360.                         while (sum>=16)
  361.                         {
  362.                            sum=sum-16;
  363.                            Carry++;
  364.                         }
  365.                         check[h]=sum;
  366.                         break;
  367.  
  368.                      case '3':
  369.                         sum=3+Carry;
  370.                         Carry=0;
  371.                         while (sum>=16)
  372.                         {
  373.                            sum=sum-16;
  374.                            Carry++;
  375.                         }
  376.                         check[h]=sum;
  377.                      case '4':
  378.                         sum=4+Carry;
  379.                         Carry=0;
  380.                         while (sum>=16)
  381.                         {
  382.                            sum=sum-16;
  383.                            Carry++;
  384.                         }
  385.                         check[h]=sum;
  386.                         break;
  387.  
  388.                      case '5':
  389.                         sum=5+Carry;
  390.                         Carry=0;
  391.                         while (sum>=16)
  392.                         {
  393.                            sum=sum-16;
  394.                            Carry++;
  395.                         }
  396.                         check[h]=sum;
  397.                         break;
  398.  
  399.                      case '6':
  400.                         sum=6+Carry;
  401.                         Carry=0;
  402.                         while (sum>=16)
  403.                         {
  404.                            sum=sum-16;
  405.                            Carry++;
  406.                         }
  407.                         check[h]=sum;
  408.                         break;
  409.  
  410.                      case '7':
  411.                         sum=7+Carry;
  412.                         Carry=0;
  413.                         while (sum>=16)
  414.                         {
  415.                            sum=sum-16;
  416.                            Carry++;
  417.                         }
  418.                         check[h]=sum;
  419.                         break;
  420.  
  421.                      case '8':
  422.                         sum=8+Carry;
  423.                         Carry=0;
  424.                         while (sum>=16)
  425.                         {
  426.                            sum=sum-16;
  427.                            Carry++;
  428.                         }
  429.                         check[h]=sum;
  430.                         break;
  431.                      case '9':
  432.                         sum=9+Carry;
  433.                         Carry=0;
  434.                         while (sum>=16)
  435.                         {
  436.                            sum=sum-16;
  437.                            Carry++;
  438.                         }
  439.                         check[h]=sum;
  440.                         break;
  441.                   }//swi
  442.                }//whi
  443.             //    }
  444.             } // the big for col
  445.  
  446.             for( g=3;g>=0;g--) // checksum
  447.             {
  448.                check[g]=15-check[g];
  449.             }
  450.             for(p=0;p<=3;p++) // in hex
  451.             {
  452.                switch(check[p])
  453.                {
  454.                   case 0:d=d+"0";
  455.                      break;
  456.                   case 1:d=d+"1";
  457.                      break;
  458.                   case 2:d=d+"3";
  459.                      break;
  460.                   case 4:d=d+"4";
  461.                      break;
  462.                   case 5:d=d+"5";
  463.                      break;
  464.                   case 6:d=d+"6";
  465.                      break;
  466.                   case 7:d=d+"7";
  467.                      break;
  468.                   case 8:d=d+"8";
  469.                      break;
  470.                   case 9:d=d+"9";
  471.                      break;
  472.                   case 10:d=d+"A";
  473.                      break;
  474.                   case 11:d=d+"B";
  475.                      break;
  476.                   case 12:d=d+"C";
  477.                      break;
  478.                   case 13:d=d+"D";
  479.                      break;
  480.                   case 14:d=d+"E";
  481.                      break;
  482.                   case 15:d=d+"F";
  483.                      break;
  484.                }// end for
  485.             }//end wihle
  486.  
  487.          System.out.println("the check sum  is " + d);
  488.  
  489.              System.out.println("the check sum  is " + d);
  490.             String [] B=new String [d.length()];
  491.  
  492.             for(int y=3;y>=d.length();y--)
  493.             {
  494.             String MN=B[y];
  495.                         }
  496.  
  497.  
  498.          System.out.println("Enter the Massege");// reciver Message
  499.          String recive=console.nextLine();
  500.          int len1=recive.length();
  501.          String q1="";
  502.  
  503.          if(len1%2==0)
  504.             for (  i1 = 0; i1 < recive.length();i1 ++)
  505.             {
  506.                U1 = recive.charAt(i1);
  507.                int j1 = (int) U1;
  508.                String hexstr = Integer.toHexString(j1); //hex 
  509.                AS1=AS1.concat(hexstr);
  510.             }//end for
  511.          else
  512.             q1=recive.concat("0");
  513.          for (  i1 = 0; i1< q1.length();i1 ++)
  514.          {
  515.                     U1 = q1.charAt(i1);
  516.             int j1 = (int) U1;
  517.             String hexstr = Integer.toHexString(j1); //hex 
  518.             AS1=AS1.concat(hexstr);
  519.          }
  520.          System.out.println("ASCII value of "+"" +" is  AS" +"   " + AS1 + ".");
  521.          System.out.println(" Full Array "); 
  522.          String[]arr11=new String[AS1.length()];
  523.          String []arr21=new String[arr11.length];
  524.  
  525.          int r1=AS1.length()/4;
  526.          int c1=4;
  527.          String [][] arr3=new String [r1][c1];
  528.          int lenn1=arr3.length;
  529.  
  530.  
  531.          for(row1 = 0 ;row1 <r1;row1++)
  532.          {
  533.             for( col1=0;col1<4;col1++)
  534.             {
  535.                char ch=AS1.charAt(4*row1+col1);
  536.                String s = String.valueOf(ch);
  537.                arr3[row1][col1]=s;
  538.                System.out.print(arr3[row1][col1]);
  539.             }
  540.             System.out.println("");
  541.  
  542.          }
  543.          for(co1=3;co1>=ro1;co1--)
  544.          {// star
  545.             for(  ro1=0;ro1<r1;ro1++)
  546.             {
  547.                ST1=arr3[ro1][co1];
  548.                char FM=ST1.charAt(0);
  549.  
  550.                switch(FM) 
  551.                {
  552.                   case '0':
  553.                      M1=0;
  554.                      break; 
  555.  
  556.                          case '1':
  557.                      M1=1;
  558.                      break;
  559.                   case '2':
  560.                      M1=2;
  561.                      break;
  562.                   case '3':
  563.                      M1=3;
  564.                      break;
  565.                   case '4':
  566.                      M1=4;
  567.                      break;
  568.                   case '5':
  569.                      M1=5;
  570.                      break;
  571.                   case '6':
  572.                      M1=7;
  573.                      break;
  574.                   case '8':
  575.                      M1=8;
  576.                      break;
  577.                   case '9':
  578.                      M1=9;
  579.                      break;
  580.                   case 'A':
  581.                   case 'a':
  582.                      M1=10;
  583.                      break; 
  584.                      case 'B':
  585.                   case 'b':
  586.                      M1=11;
  587.                      break;
  588.                   case 'C':
  589.                   case 'c':
  590.                      M1=12;
  591.                      break;
  592.                   case 'D':
  593.                   case 'd':
  594.                      M1=13;
  595.                      break;
  596.                   case 'E':
  597.                   case 'e':
  598.                      M1=14;
  599.                      break;
  600.                   case 'F':
  601.                   case 'f':
  602.                      M1=15;
  603.                      break;
  604.                }
  605.                      for(int y=3;y>=d.length();y--)
  606.                      {
  607.                      String MN=B[y];
  608.  
  609.                switch(MN.charAt(0))   
  610.                {
  611.                   case '0':
  612.                      R=0;
  613.                      break; 
  614.                   case '1':
  615.                      R=1;
  616.                      break;
  617.                   case '2':
  618.                      R=2;
  619.                      break;
  620.                   case '3':
  621.                      R=3;
  622.                      break;
  623.                   case '4':
  624.                      R=4;
  625.                      break;
  626.                   case '5':
  627.                      R=5;
  628.                      break;
  629.                   case '6':
  630.                      R=7;
  631.                      break;
  632.                   case '8':
  633.                      R=8;
  634.                      break;
  635.                   case '9':
  636.                      R=9;
  637.                      break;
  638.                   case 'A':
  639.                   case 'a':
  640.                      R=10;
  641.                      break; 
  642.                   case 'B':
  643.                   case 'b':
  644.                      R=11;
  645.                      break;
  646.                   case 'C':
  647.                   case 'c':
  648.                      R=12;
  649.                      break;
  650.                   case 'D':
  651.                   case 'd':
  652.                      R=13;
  653.                      break;
  654.                   case 'E':
  655.                   case 'e':
  656.                      R=14;
  657.                      break;
  658.                   case 'F':
  659.                   case 'f':
  660.                      R=15;
  661.                      break;
  662.                }//end switch
  663.  
  664.                sum1=sum1+M1+R;
  665.                sum1=sum1+carry1; 
  666.                carry1=0;
  667.                     }// end for
  668.                     }
  669.                System.out.println("The sum "+"   "+ "=" +sum1);
  670.  
  671.                while(sum1>=16)
  672.                {
  673.                   sum1=sum1-16;
  674.                   Carry1++;
  675.                   System.out.println("the carry is " + Carry1);
  676.                         }//end while
  677.                        switch(sum1)
  678.             {
  679.                case 0:CH1[index1]="0";
  680.                   break;
  681.                case 1:CH1[index1]="1";
  682.                   break;
  683.                case 2:CH1[index1]="2";
  684.                   break;
  685.                case 3:CH1[index1]="3";
  686.                   break;
  687.                case 4:CH1[index1]="4";
  688.                   break;
  689.                case 5:CH1[index1]="5";
  690.                   break;
  691.                case 6:CH1[index1]="6";
  692.                   break;
  693.                case 7:CH1[index1]="7";
  694.  
  695.                   break;
  696.                case 8:CH1[index1]="8";
  697.                   break;
  698.                case 9:CH1[index1]="9";
  699.                   break;
  700.                case 10:CH1[index1]="A";
  701.                   break;
  702.                case 11:CH1[index1]="B";
  703.                   break;
  704.                case 12:CH1[index1]="C"; 
  705.                   break;
  706.                case 13:CH1[index1]="D";
  707.                   break;
  708.                case 14:CH1[index1]="E";
  709.                   break;
  710.                case 15:CH1[index1]="F";
  711.                   break;
  712.             }
  713.             index1--;
  714.                 sumw1=Integer.toString(sum);
  715.                ro1=0;
  716.             sum1=0;
  717.          }//end wh
  718.                   while(Carry1 !=0)
  719.          {
  720.             for( h1=3;h1>=0;h1--)
  721.             { 
  722.                 CH1[h1]=sumw1;
  723.                   switch(sumw1.charAt(0))
  724.                {
  725.                   case 'A':
  726.                   case 'a':
  727.                      sum1=10+Carry1;
  728.                      Carry1=0;
  729.                      while (sum1>=16)
  730.                      {
  731.                         sum1=sum1-16;
  732.                         Carry1++;
  733.                      }
  734.                      check1[h1]=sum1;
  735.                      break;
  736.                   case 'B':
  737.                   case 'b':
  738.                      sum1=11+Carry1;
  739.                      Carry1=0;
  740.                      while (sum1>=16)
  741.                      {
  742.                         sum1=sum1-16;
  743.                         Carry1++;
  744.                      }
  745.                      check1[h1]=sum1;
  746.                      break;
  747.                   case 'C':
  748.                   case 'c':
  749.                      sum1=12+Carry1;
  750.                      Carry1=0;
  751.                      while (sum1>=16)
  752.                      {
  753.                         sum1=sum1-16;
  754.                         Carry1++;
  755.                      }
  756.                      check1[h1]=sum1;
  757.                      break;
  758.                   case 'D':
  759.                   case 'd':
  760.                      sum1=13+Carry1;
  761.                      Carry1=0;
  762.                      while (sum1>=16)
  763.                      {
  764.                         sum1=sum1-16;
  765.                         Carry1++;
  766.                      }
  767.                      check1[h1]=sum1;
  768.                      break;
  769.                   case 'E':
  770.                   case 'e':
  771.                      sum1=14+Carry1;
  772.                      Carry1=0;
  773.                      while (sum1>=16)
  774.                      {
  775.                         sum1=sum1-16;
  776.                         Carry1++;
  777.                      }
  778.                      check1[h1]=sum1;
  779.                      break;
  780.                   case 'F':
  781.                   case 'f':
  782.                      sum1=15+Carry1;
  783.                      Carry1=0;
  784.                      while (sum1>=16)
  785.                      {
  786.                         sum1=sum1-16;
  787.                         Carry1++;
  788.                      }
  789.                      check1[h1]=sum1;
  790.                      break;
  791.                   case '0':
  792.                      sum1=0+Carry1;
  793.                      Carry1=0;
  794.                      while (sum1>=16)
  795.                      {
  796.                         sum1=sum1-16;
  797.                         Carry1++;
  798.                      }
  799.                      check1[h1]=sum1;
  800.                      break;
  801.                   case '1':
  802.                      sum1=1+Carry1;
  803.                      Carry1=0;
  804.                      while (sum1>=16)
  805.                      {
  806.                         sum1=sum1-16;
  807.                         Carry1++;
  808.                      }
  809.                      check1[h1]=sum1;
  810.                      break;
  811.  
  812.                   case '2':
  813.                      sum1=2+Carry1;
  814.                      Carry1=0;
  815.                      while (sum1>=16)
  816.                      {
  817.                         sum1=sum1-16;
  818.                         Carry1++;
  819.                      }
  820.                      check1[h1]=sum1;
  821.                      break;
  822.  
  823.                   case '3':
  824.                      sum1=3+Carry1;
  825.                      Carry1=0;
  826.                      while (sum1>=16)
  827.                      {
  828.                         sum1=sum1-16;
  829.                         Carry1++;
  830.                      }
  831.                      check1[h1]=sum1;
  832.                   case '4':
  833.                      sum1=4+Carry1;
  834.                      Carry1=0;
  835.                      while (sum1>=16)
  836.                      {
  837.                         sum1=sum1-16;
  838.                         Carry1++;
  839.                      }
  840.                      check1[h1]=sum1;
  841.                      break;
  842.  
  843.                   case '5':
  844.                      sum1=5+Carry1;
  845.                      Carry1=0;
  846.                      while (sum1>=16)
  847.                      {
  848.                         sum1=sum1-16;
  849.                         Carry1++;
  850.                      }
  851.                      check1[h1]=sum1;
  852.                      break;
  853.  
  854.                   case '6':
  855.                      sum1=6+Carry1;
  856.                      Carry1=0;
  857.                      while (sum1>=16)
  858.                      {
  859.                         sum1=sum1-16;
  860.                         Carry1++;
  861.                      }
  862.                      check1[h1]=sum1;
  863.                      break;
  864.  
  865.                   case '7':
  866.                      sum1=7+Carry1;
  867.                      Carry1=0;
  868.                      while (sum1>=16)
  869.                      {
  870.                         sum1=sum1-16;
  871.                         Carry1++;
  872.                      }
  873.                      check1[h1]=sum1;
  874.                      break;
  875.  
  876.                   case '8':
  877.                      sum1=8+Carry1;
  878.                      Carry1=0;
  879.                      while (sum1>=16)
  880.                      {
  881.                         sum1=sum1-16;
  882.                         Carry1++;
  883.                      }
  884.                      check1[h1]=sum1;
  885.                      break;
  886.                   case '9':
  887.                      sum1=9+Carry1;
  888.                      Carry1=0;
  889.                      while (sum1>=16)
  890.                      {
  891.                         sum1=sum1-16;
  892.                         Carry1++;
  893.                      }
  894.                      check1[h1]=sum1;
  895.                   }   break;
  896.                //}
  897.             }
  898.          }// bigest for
  899.          for( g1=3;g1>=0;g1--)
  900.          {
  901.             check1[g1]=15-check1[g1];
  902.          }
  903.  
  904.          for(p1=0;p1<=3;p1++)
  905.          {
  906.             switch(check1[p1])
  907.             {
  908.                case 10:d1=d1+"A";
  909.                   break;
  910.                case 11:d1=d1+"B";
  911.                   break;
  912.                case 12:d1=d1+"C";
  913.                   break;
  914.                case 13:d1=d1+"D";
  915.                   break;
  916.                case 14:d1=d1+"E";
  917.                   break;
  918.                case 15:d1=d1+"F";
  919.                   break;
  920.                case 0:d1=d1+"0";
  921.                   break;
  922.                case 1:d1=d1+"1";
  923.                   break;
  924.                case 2:d1=d1+"2";
  925.                   break;
  926.                case 3:d1=d1+"3";
  927.                   break;
  928.                case 4:d1=d1+"4";
  929.                   break;
  930.                case 5:d1=d1+"5";
  931.                   break;
  932.                case 6:d1=d1+"6";
  933.                   break;
  934.                case 7:d1=d1+"7";
  935.                   break;
  936.                case 8:d1=d1+"8";
  937.                   break;
  938.                case 9:d1=d1+"9";
  939.                   break;
  940.             }// end if
  941.          }//end wihle
  942.          System.out.println("the check sum is " + d1);// is check sum
  943.          if(d1.equals("0000"))
  944.             System.out.println(" The Message is correct");
  945.          else
  946.             System.out.println(" The Message is not correct");
  947.       }
  948.  
  949. }
  950.  
Feb 15 '12 #1
Share this Question
Share on Google+
1 Reply


P: 5
plz , can anyone help me :"""(
Feb 16 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.