简体   繁体   中英

How do I add an integer to a char in Java?

I'm making a Cipher in Java but I can't do the follwing thing: I want to edit the value of a char array to encrypt it

How should I do this?

Changing the ASCII value of each char might be the answer, I don't know; that's why I'm asking you Guys!

You can add an int to a char , but the result is an int - you'd have to cast back to char to put it back in the array, unless you use the compound assignment operator:

array[x] += someInt;

or

array[x] = (char) (array[x] + someInt);

However, usually this isn't an appropriate way of performing encryption. You'll often end up with unprintable characters, or characters which don't even have a specific meaning in Unicode. Instead, most encryption algorithms are designed to work on arbitrary binary data - ie byte arrays.

Usually you would convert the string into a byte array first (eg with String.getBytes(charset) - definitely specify an encoding). Then perform encryption on the byte array, giving you a new byte array. If you really need to convert it back to text, use base64 to do so - do not use new String(encryptedBytes) , as you no longer have text data encoded in a normal text encoding.

In Java, char and int are compatible types so just add them with + operator.

char c = 'c';
int x = 10;

c + x results in an integer, so you need an explicit casting to assign it to your character varaible back.

c = (char)(c + x); // After this assignment, c = 'm'

The simple symmetric rotation cipher uses the same function both to encode as well as to decode the data when using a 26 letter alphabet.

public class Rot13 { 
    public static void main(String[] args) {
        String s = args[0];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if       (c >= 'a' && c <= 'm') c += 13;
            else if  (c >= 'A' && c <= 'M') c += 13;
            else if  (c >= 'n' && c <= 'z') c -= 13;
            else if  (c >= 'N' && c <= 'Z') c -= 13;
            System.out.print(c);
        }
        System.out.println();
    }
}

Sourcehere .

In the above example there is no need for the cast to char because of the "add and assign" operator that stores the result within the initial container ( char c ) which is a char.

Add the int value with the char and typecast back to char as below:

  char a = 'a';
  char b = (char)(a+5);
  System.out.println(b);  //<-prints "f"

In char array,

     charArray[index] = (char)(charArray[index] + intValue);

 public class IntPos { static int dim = 4; public static String[][] main(){ String[][] p = new String[dim][dim]; char letter = 'A'; for(int i = 0; i < dim;i++){ for(int j = 0; j<dim;j++){ char newLetter =(char)(letter + i); p[i][j] = Character.toString(newLetter)+j; System.out.println(p[i][j]); } } return p; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM