Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Iron Lion

Thread: Is this a sensible way to create a Checksum?

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default Is this a sensible way to create a Checksum?

    Given a certain string, I need to "sum" the ASCII value of all its character elements, then convert that to a value no greater than 255, then append the resulting checksum value as a two-character hex val to the original value along with a left brace.

    IOW, given the value "0000017^E^^" I need to end up with "0000017^E^^B7["

    Is this code a sensible way to do this, or am I going about this all wrong?

    Java Code:
    	public static String AppendChecksum(String AStr) {
    		int iCharValTotals = GetCharValTotals(AStr);
    		byte bite = (byte) (iCharValTotals);
    		bite = (byte) (bite & 0xFF);
    		int iCharValsReduced = (int)(bite);
    		String sChecksum = Integer.toHexString(iCharValsReduced);
    		return String.format("%s%s[", AStr, sChecksum);
    	}
    
    	private static int GetCharValTotals(String AStr) {
    		char currentChar = ' ';
    		int iASCIIVal = 0;
    		int iASCIIValsTotal = 0;
    
    		for (int i = 0; i < AStr.length(); i++) {
    			currentChar = AStr.charAt(i);
    			iASCIIVal = (int) currentChar;
    			iASCIIValsTotal = iASCIIValsTotal + iASCIIVal;
    		}
    		return iASCIIValsTotal;
    	}

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default

    Since the range of a byte is -128 to 127, and your spec calls for a max of 255, I'd suggest changing it to:

    Java Code:
    		int iCharValTotals = GetCharValTotals(AStr);
    		iCharValTotals &= 0xFF;
    		String sChecksum = Integer.toHexString(iCharValTotals);
    		// ...
    blackbird likes this.

Similar Threads

  1. Create download url
    By contest man in forum New To Java
    Replies: 1
    Last Post: 03-16-2011, 08:13 PM
  2. create id
    By daisy in forum JDBC
    Replies: 15
    Last Post: 03-16-2011, 10:34 AM
  3. jsp to create xml
    By mcajavaprogramer in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2010, 01:22 PM
  4. How to create the Jvm using JDK?
    By Sungron in forum New To Java
    Replies: 4
    Last Post: 02-01-2010, 11:50 AM
  5. Create XML From XSD
    By Jack in forum XML
    Replies: 1
    Last Post: 07-09-2007, 01:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •