C++ 得到一个;在抛出';nlohmann::detail::type#u error';what():索引0:0x81处的UTF-8字节无效;
我正在处理一个网站上的编码问题,当我编译代码时,它会给我: 在抛出的实例后调用terminate 'nlohmann::detail::type_error'what(): [json.exception.type_error.316]索引0处的UTF-8字节无效:0x81 中止退出状态134 然而,当我在Sublime上编译时,它可以很好地使用正确的输出。我使用ASCII值存储到字符串变量C++ 得到一个;在抛出';nlohmann::detail::type#u error';what():索引0:0x81处的UTF-8字节无效;,c++,compiler-errors,runtime-error,C++,Compiler Errors,Runtime Error,我正在处理一个网站上的编码问题,当我编译代码时,它会给我: 在抛出的实例后调用terminate 'nlohmann::detail::type_error'what(): [json.exception.type_error.316]索引0处的UTF-8字节无效:0x81 中止退出状态134 然而,当我在Sublime上编译时,它可以很好地使用正确的输出。我使用ASCII值存储到字符串变量answer中的方式是否有问题?这是我的密码: string caesarCypherEncryptor(
answer
中的方式是否有问题?这是我的密码:
string caesarCypherEncryptor(string str, int key) {
string answer = "";
for(char letter : str) {
// if it goes over 'z': get amount pass 'z' and start at 'a'
if(letter + key > int('z')) {
// push back char into answer string
answer += ((letter + key) % int('z') + int('a'));
continue;
}
// else just add key from current position
answer += letter + key;
}
return answer;
}
int main() {
cout << caesarCypherEncryptor("mvklahvjcnbwqvtutmfafkwiuagjkzmzwgf", 7) << endl;
return 0;
}
string-caesarCypherEncryptor(string-str,int-key){
字符串答案=”;
for(字符:str){
//如果超过“z”:获取金额通过“z”并从“a”开始
如果(字母+键>整数('z')){
//将字符推回应答字符串
答案+=((字母+键)%int('z')+int('a'));
继续;
}
//否则只需从当前位置添加关键点
答案+=字母+钥匙;
}
返回答案;
}
int main(){
我也面临这个问题。
这基本上发生在我们将int添加到字符和最终ascii值交叉127时。因此,ascii值会滚动。即'z'+7=129
-但它会滚动并变成-129-127=2
基本上,您需要使用以下条件:
if((unsigned int)(letter + key) > 'z')
请试试这个,如果有帮助的话,请告诉我。但是,当我对崇高进行编译时,它工作得很好。我没有看到任何程序。我看到了一个函数。<代码>主< <代码>函数,样本数据,调用这个函数?这不是一个,<代码>崇高> <代码>不是C++编译器。它是一个代码编辑器和IDE。它是编译的。如果
char
是有符号的或无符号的,则与er有关。有符号整数类型的算术溢出会导致未定义的行为。如果char
是有符号的,则例如'z'+7
将溢出。如果显式指定无符号(如无符号char letter
)行为是什么?我得到了与无符号字符相同的结果。请再次阅读错误消息。它说什么?从哪里报告?当您有一个字符不是有效的UTF-8字符,然后尝试将其作为UTF-8字符传递时,您认为会发生什么?请记住ASCII字母表是七位的编码系统,UTF-8对前127个字符使用ASCII。高于0x80
的字符值作为单个字符无效。