C++ 加密 ascii 值打印数组中的返回键

标签 c++ arrays loops casting ascii

我有这个功能。它的目标是首先取出数组的最后一个字符,如果它是字母,则将其大写。如果它是返回键(ASCII 值 10 )或空行,也将其打印出来。所有其他字符,请勿打印。请注意我的 Sentinel_value = 10。除了我的 else 语句之外,它工作正常。它不打印返回键。输出全部在一行上。有什么建议吗?

void EncryptMessage (ofstream& outFile, char charArray[], int length)
{
    int index;
    int asciiValue;
    int asciiValue2;
    char upperCased;
    char finalChar;


    for (index = length-1; index >= 0 ; --index)
    {
        upperCased = static_cast<char>(toupper(charArray[index]));
        if (upperCased >= 'A' && upperCased <= 'Z')
        {
            asciiValue = static_cast<int>(upperCased) - 10;
            finalChar = static_cast<char>(asciiValue);  
            outFile << finalChar;
        }
        else
        {
            asciiValue2 = static_cast<int>(charArray[index]);
            if (asciiValue2 == SENTINEL_VALUE)
            {
                outFile << asciiValue2;
            }   
        }
    }
}

最佳答案

asciiValue2 是一个 int,因此它的 ASCII 值被插入到流中(两个字符,“1”和“0”),而不是它的字符表示形式。将 asciiValue2 声明为 char 就可以了。

关于C++ 加密 ascii 值打印数组中的返回键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10015729/

相关文章:

C++ 分配一个变量指针地址

c++ - 为什么我不能使用函数调用来声明数组的长度?

javascript - 字符串是...约定之一

java - 以板格式将 2D 数组打印到控制台

java - 在 do-while 循环中使用 JSoup,仍然抛出异常

c++ - Qt 4.7 - 工具提示和 QComboBox 弹出窗口在 OS X 10.12 下出现空白

C++ XOR 加密/解密(异或密码)对我不起作用

c++ - 迭代与。枚举

从 C 中的另一个数组创建数组

PHP 如何在条形图/图形中打印字符串中的字符数?