从 char 中减去 int:int 到 char 的可能有损转换 [英] Subtract int from char : possible lossy conversion int to char

查看:26
本文介绍了从 char 中减去 int:int 到 char 的可能有损转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在典型的密码学密钥问题中从 char 中减去 int,但我在以下语句中遇到了上述错误:

I am trying to subtract int from char in a typical cryptography key question but I am running into the above mentioned error in the following statements:

char ch = (int)encrypted_message.charAt(i) + key[index];
if (ch > 122)
    ch = (int)ch - 26;

key[] 数组保存密钥的数字,类型为 int.

key[] array holds digits of the key and is of type int.

如何成功旋转字符?

请帮忙!

推荐答案

intchar 大,但是你的操作结果是int(然后将其存储在 char 中).因此,编译器警告您可能会丢失将 int 值存储到 char 变量中的信息.

int is bigger than char, but the result of your operation is typed int (which you're then storing in a char). So the compiler is warning you you might lose information storing an int value into a char variable.

相反,确保结果是 char,这有点麻烦,因为 +- 带有 char 值导致 int,所以我们必须转换:

Instead, ensure the result is a char, which is a bit of a pain because + and - with char values results in an int, so we have to cast:

char ch = (char)(encrypted_message.charAt(i) + key[index]);
if (ch > 122)
    ch = (char)(ch - 26);

这篇关于从 char 中减去 int:int 到 char 的可能有损转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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