简体   繁体   English

字符串移位

[英]Bit-Shift a String

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? 那么如何将String按int的值进行位移?

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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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