从ASCII值转换为Char [英] Conversion from ASCII values to Char

查看:1003
本文介绍了从ASCII值转换为Char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String source = "WEDGEZ"
char letter = source.charAt(i);
shift=5;
for (int i=0;i<source.length();i++){
if (source.charAt(i) >=65 && source.charAt(i) <=90 )
  letterMix =(char)(('D' + (letter - 'D' + shift) % 26));
}

好吧,我想要做的是拿字符串WEDGEZ,将每个字母移5,这样W变成B,E变成J,依此类推.但是我感觉与我使用的数字有些矛盾.

Ok what I'm trying to do is take the string WEDGEZ, and shift each letter by 5, so W becomes B and E becomes J, etc. However I feel like there is some inconsistency with the numbers I'm using.

对于if语句,我使用的是ASCII值,对于 letterMix= statement,我使用的是1-26中的数字(我认为).实际上,问题也与此有关:

For the if statement, I'm using ASCII values, and for the letterMix= statement, I'm using the numbers from 1-26 (I think). Well actually, the question is about that too:

是什么 (char)(('D' + (letter - 'D' + shift) % 26));反正回来吗?它返回一个char权限,但是是从int转换而来的.我在网上发现该声明,但我自己并没有完全撰写它,因此该声明究竟返回了什么.

What does (char)(('D' + (letter - 'D' + shift) % 26)); return anyway? It returns a char right, but converted from an int. I found that statement online somewhere I didn't compose it entirely myself so what exactly does that statement return.

此代码的一般问题是,对于W,它返回'/',对于Z,它返回_,我猜这意味着它正在使用ASCII值.我真的不知道该如何处理.

The general problem with this code is that for W it returns '/' and for Z it returns _, which I'm guessing means it's using the ASCII values. I really dont know how to approach this.

修改:新代码

    for (int i=0;i<source.length();i++)
        {
        char letter = source.charAt(i);
        letterMix=source.charAt(i);
        if (source.charAt(i) >=65 && source.charAt(i) <=90 ){
            letterMix=(char)('A' + (  ( (letter - 'A') + input ) % 26));
            }
        }

推荐答案

好吧,我不确定是否要完成此作业,所以我会对代码感到小气.

Well I'm not sure if this homework, so i'll be stingy with the Code.

您正在编写凯撒密码,其偏移为5.

You're Writing a Caesar Cipher with a shift of 5.

要解决您的Z-> _问题...我假设您希望将所有字母都更改为编码后的字母(而不是奇怪的符号).问题是A-Z的ASCII值在65到90之间. 当对Z进行编码时(例如),最终将其加5,从而给u值95(_).

To address your Z -> _ problem...I'm Assuming you want all the letters to be changed into encoded letters (and not weird Symbols). The problem is ASCII values of A-Z lie between 65 and 90. When coding Z (for eg), you end up adding 5 to it, which gives u the value 95 (_).

您需要做的是将可用的字母环绕起来.首先隔离字符在字母中的相对位置(即A = 0,B = 1 ...),您需要减去65(即A的ASCII.添加您的Shift,然后应用modulus 26 .这将使您的价值回绕.

What you need to do is Wrap around the available alphabets. First isolate, the relative position of the character in the alphabets (ie A = 0, B = 1 ...) You Need to subtract 65 (which is ASCII of A. Add your Shift and then apply modulus 26. This will cause your value to wrap around.

例如,它是您的编码Z,(ASCII = 90),因此相对位置是25(= 90-65). 现在,25 + 5 = 30,但是您需要将该值限制在26以内.因此,您将modulus 26 所以30 % 264就是E.

eg, it your encoding Z, (ASCII=90), so relative position is 25 (= 90 - 65). now, 25 + 5 = 30, but you need the value to be within 26. so you take modulus 26 so 30 % 26 is 4 which is E.

就在这里

char letter = message(i);
int relativePosition = letter - 'A'; // 0-25
int encode = (relativePosition + shift) % 26
char encodedChar = encode + 'A' // convert it back to ASCII.

所以一行,

char encodedChar = 'A' + (  ( (letter - 'A') + shift ) % 26)

注意,这仅适用于大写字母,如果您打算使用小写字母,则需要进行一些额外的处理.

Note, This will work only for upper case, if your planning to use lower case, you'll need some extra processing.

您可以使用Character.isUpperCase()检查大写字母.

You can use Character.isUpperCase() to check for upper case.

这篇关于从ASCII值转换为Char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆