Reputation: 919
Im working on a simple encryptor for class and what Im trying to do is take in a message from the user as a String, scramble it then save the scrambled messege. I can figure out most parts of this, except that I would like to scramble the String by bit-shifting all the chars by a user set value.
So say I have:
String msg="hello my name is blah blah";
int userKey=6;
So how would I bit-shift the String by the value of the int?
Upvotes: 0
Views: 6297
Reputation: 16526
Assuming you're trying to shift every char individually, you can try this snippet.-
StringBuilder msg = new StringBuilder("hello my name is blah blah");
int userKey = 6;
for (int i = 0; i < msg.length(); i ++) {
msg.setCharAt(i, (char) (msg.charAt(i) + userKey));
}
Upvotes: 3