Domanda

I'm trying to work out a checksum from a string such as this "[Y-34-]" the checksum should add up all the characters in the string. I'm struggling to understand how to add up numbers to letters and other character.

Do characters as [,Y and - need to be converted into their ascii? values? in that case. How would I convert them and then add them to the number?

thanks

È stato utile?

Soluzione

You can use

int sum=0;
   for (int i=0;i<string.length;i++){
      sum=sum+(int)string.charAt(i);
    }

This works because internally a char is stored as a number which you can easily add up and get an int.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top