尝试打印所有ASCII值时出现无限循环

时间:2019-10-13 01:49:41

标签: loops ascii infinite

当我尝试打印所有128个ASCII值时,我似乎陷入了无限循环,但我不明白为什么。这个问题要求我编写一个程序,该程序使用循环显示ASCII代码0到127的字符,并在每行上显示16个字符。我尝试使用for循环,并告诉程序如果我的char变量超过127,则停止循环,但由于某种原因,我最终遇到了无限循环。我什至写了一个if语句,告诉它如果char变量超过127则循环中断,但这似乎也不起作用。这是我的代码,非常感谢您提供任何反馈意见。

filter := bson.D{
    {"sessionid", "C-dhf"},
    {"$or", bson.A{
        bson.D{{"family", primitive.Regex{Pattern: "^s", Options: "i"}}},
        bson.D{{"given", primitive.Regex{Pattern: "^n", Options: "i"}}},
    }},
}

3 个答案:

答案 0 :(得分:2)

您的julia> prnt("this is programming") thisisprogramming 类型几乎可以肯定是已签名的类型(C / C ++允许对char进行签名或未签名(a)),因此,当您在{ {1}},它环绕到char(实际上不是标准的强制,而是常见的行为)。

因此,它总是 小于或等于127

您可以使用以下代码进行测试:

-128

在这种情况下,只需使用127而不是#include <iostream> using namespace std; int main() { char x = 127; x++; cout << (int)x << endl; return 0; }


这里有一些代码可以完成这项工作,同时也只打印出专门标记为可打印的字符:

unsigned char

结果是:

char

(a)来自#include <iostream> using namespace std; int main() { unsigned char a = 0; while (a < 128) { if (isprint(a)) { cout << a << " "; } else { cout << ". "; } if (++a % 16 == 0) { cout << endl; } } return 0; }

  

一个普通的. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ . 对象可以采用与C++ 17, basic.fundamentalchar相同的值;哪个是实现定义的。

答案 1 :(得分:0)

您的问题是function getFileContentsP(path, contents) { $.post("/os/php/file_get_contents.php", {"path": path}, function (data, status, jqXHR) { contents = data; }, ); return contents; } 数据类型的范围是-128到127。这也意味着当您到达<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { var $filePath = testInput($_POST["path"]); // returns file contents echo file_get_contents($filePath); } function testInput($input) { $input = trim($input); //$input = stripslashes($input); filepaths have slashes $input = htmlspecialchars($input); return $input; } ?> 然后进入char时,它会循环回到{{ 1}},然后继续。因此,当您查看a = 127的条件时,它将始终为真。一种选择可能是创建无符号类型

答案 2 :(得分:-1)

char是一个符号, 所以a + 128 = a

这意味着当a == 127时,a ++给您128-> a = 0,因此条件仍然为真

您可以使用=修复它 如果(a> = 127)中断;

或在(a!= 127)时使用do {}